Skip to content

Commit

Permalink
Adjust the start and end offset to exclude leading and trailing space…
Browse files Browse the repository at this point in the history
…s. Also add unit tests
  • Loading branch information
FAMarfuaty committed Jan 2, 2025
1 parent 322b221 commit d09f012
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ describe( "An assessment for sentence length", function() {
new Mark( {
position: {
startOffset: 0,
endOffset: 81,
endOffset: 80,
startOffsetBlock: 0,
endOffsetBlock: 81,
endOffsetBlock: 80,
clientId: "",
attributeId: "",
isFirstSection: false,
Expand Down Expand Up @@ -626,16 +626,59 @@ describe( "A test for marking too long sentences", function() {
expect( new SentenceLengthInTextAssessment().getMarks( mockPaper, mockResearcher ) ).toEqual( expected );
} );

it( "adjusts the startOffset when the long sentence starts with a space", () => {
const mockPaper = new Paper( " This is a too long sentence, because it has over twenty words, and that is hard too read, don't you think?" );
const mockResearcher = new EnglishResearcher( mockPaper );
buildTree( mockPaper, mockResearcher );

const expected = [
new Mark( {
position: {
startOffset: 1,
endOffset: 107,
startOffsetBlock: 1,
endOffsetBlock: 107,
attributeId: "",
clientId: "",
isFirstSection: false,
} } ),
];
expect( new SentenceLengthInTextAssessment().getMarks( mockPaper, mockResearcher ) ).toEqual( expected );
} );

it( "adjusts the endOffset when the long sentence ends with a space", () => {
const mockPaper = new Paper( "This is a too long sentence, because it has over twenty words, and that is hard too read, don't you think? " );
const mockResearcher = new EnglishResearcher( mockPaper );
buildTree( mockPaper, mockResearcher );

const expected = [
new Mark( {
position: {
startOffset: 0,
endOffset: 106,
startOffsetBlock: 0,
endOffsetBlock: 106,
attributeId: "",
clientId: "",
isFirstSection: false,
} } ),
];
expect( new SentenceLengthInTextAssessment().getMarks( mockPaper, mockResearcher ) ).toEqual( expected );
} );

it( "returns no markers if no sentences are too long", function() {
const paper = new Paper( "This is a short sentence." );
const sentenceLengthInText = Factory.buildMockResearcher( [ { sentence: "This is a short sentence.", sentenceLength: 5 } ] );
// const sentenceLengthInText = Factory.buildMockResearcher( [ { sentence: "This is a short sentence.", sentenceLength: 5 } ] );
const mockResearcher = new EnglishResearcher( paper );
buildTree( paper, mockResearcher );

const expected = [];
expect( new SentenceLengthInTextAssessment().getMarks( paper, sentenceLengthInText ) ).toEqual( expected );
expect( new SentenceLengthInTextAssessment().getMarks( paper, mockResearcher ) ).toEqual( expected );
} );
} );

describe( "A test for marking too long sentences", function() {
it( "calculatePercentage returns nothing if there are no sentences", function() {
describe( "A test for calculatePercentage", function() {
it( "returns nothing if there are no sentences", function() {
expect( new SentenceLengthInTextAssessment().calculatePercentage( [] ) ).toEqual( 0 );
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,18 @@ class SentenceLengthInTextAssessment extends Assessment {

return tooLongSentences.map( tooLongSentence => {
const sentence = tooLongSentence.sentence;
const startOffset = sentence.sourceCodeRange.startOffset;
const endOffset = sentence.sourceCodeRange.endOffset;
const { text } = sentence;

let startOffset = sentence.sourceCodeRange.startOffset;
let endOffset = sentence.sourceCodeRange.endOffset;
// Adjust the start and end offset to exclude leading and trailing spaces.
if ( text.startsWith( " " ) ) {
startOffset += 1;
}
if ( text.endsWith( " " ) ) {
endOffset -= 1;
}

return new Mark( {
position: {
startOffset,
Expand Down

0 comments on commit d09f012

Please sign in to comment.