Skip to content

Commit

Permalink
add expected behavior tests and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hasanheroglu committed Oct 10, 2022
1 parent e49afd9 commit e93b007
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"id2": "urn:typoCheck",
"x@context":"https://www.w3.org/2022/wot/td/v1.1",
"@context":"https://www.w3.org/2022/wot/td/v1.1",
"title": "MyLampThing",
"description":"Valid TD demonstrating how to use security in interaction level by overwriting the root level security",
"securityDefinitins": {
"securityDefinitions": {
"basic_sc": {
"scheme": "basic",
"in": "header"
Expand All @@ -15,7 +15,7 @@
"scheme": "nosec"
}
},
"securityuu": ["basic_sc"],
"security": ["basic_sc"],
"propeRties": {
"status" : {
"type": "string",
Expand All @@ -42,5 +42,6 @@
"security": ["nosec_sc"]
}]
}
}
},
"typoCount": 4
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@
"security": ["nosec_sc"]
}]
}
}
},
"typoCount": 0
}
20 changes: 11 additions & 9 deletions packages/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -953,13 +953,12 @@ const PROPERTIES = "properties"
const ADDITONAL_PROPERTIES = "additional_properties"
const DATA_SCHEMA = "dataSchema"
const PATH = "#/"
const JSON_SCHEMA = require('./td-schema.json')
const TYPO_LOOKUP_TABLE = createSchemaLookupTable(JSON_SCHEMA)
const TYPO_LOOKUP_TABLE = createSchemaLookupTable(tdSchema)

/**
* Checks possible typos in a TD
* @param {object} td The TD to apply typo check on
* @returns List of possible typos
* @returns List of possible typos where the typo consists of string value of typo itself and the message, another string value, to be prompted to the user for the fix
*/
function checkTypos(td) {
const typos = []
Expand Down Expand Up @@ -1061,7 +1060,7 @@ function findPathsInSchema(lookupTable, schema, path) {
path = 'r' + path
}

findPathsInSchema(getRefObjectOfSchema(JSON_SCHEMA, schema[REF]), path)
findPathsInSchema(getRefObjectOfSchema(tdSchema, schema[REF]), path)
return
}

Expand All @@ -1078,7 +1077,7 @@ function findPathsInSchema(lookupTable, schema, path) {
path = 'r' + path
}

findPathsInSchema(getRefObjectOfSchema(JSON_SCHEMA, properties[key]), path)
findPathsInSchema(getRefObjectOfSchema(tdSchema, properties[key]), path)
return
} else {
findPathsInSchema(properties[key], `${path}${key}/`)
Expand All @@ -1099,7 +1098,7 @@ function findPathsInSchema(lookupTable, schema, path) {
path = 'r' + path
}

findPathsInSchema(getRefObjectOfSchema(JSON_SCHEMA, additionalProperties[key]), `${path}*/`)
findPathsInSchema(getRefObjectOfSchema(tdSchema, additionalProperties[key]), `${path}*/`)
return
}
}
Expand All @@ -1122,7 +1121,7 @@ function findPathsInSchema(lookupTable, schema, path) {
path = 'r' + path
}

findPathsInSchema(getRefObjectOfSchema(JSON_SCHEMA, items[item]), path)
findPathsInSchema(getRefObjectOfSchema(tdSchema, items[item]), path)
return
}
}
Expand Down Expand Up @@ -1187,8 +1186,11 @@ function getRefObjectOfSchema(schema, ref) {
return result
}

// Minimum similarity value to be able to say that two words are similar
const SIMILARITY_THRESHOLD = 0.85
const LENGTH_DIFFERENCE_THRESHOLD = 2

// Maximum value of length difference between two words
const MAX_LENGTH_DIFFERENCE = 2

/**
* Checks whether typo exists or not by comparing similarity of the two words
Expand All @@ -1197,7 +1199,7 @@ const LENGTH_DIFFERENCE_THRESHOLD = 2
* @returns Boolean value that tell whether typo exists or not
*/
function doesTypoExist(actual, desired) {
if (Math.abs(actual.length - desired.length) > LENGTH_DIFFERENCE_THRESHOLD) {
if (Math.abs(actual.length - desired.length) > MAX_LENGTH_DIFFERENCE) {
return false
}

Expand Down
43 changes: 43 additions & 0 deletions packages/core/tests/examples-typo.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const fs = require("fs")
const path = require("path")
const { checkTypos } = require("../index")
const tdValidator = require("../index").tdValidator

const rootDir = path.join("./", "examples", "tds")

const dirPath = path.join(rootDir, "typo")
const fileNames = fs.readdirSync(dirPath)
const refResult = {
report: {
json: 'passed',
schema: 'passed',
defaults: expect.stringMatching(/warning|passed/),
jsonld: 'passed',
additional: expect.stringMatching(/warning|passed/)
},
details: {
enumConst: expect.stringMatching(/failed|warning|passed/),
propItems: expect.stringMatching(/failed|warning|passed/),
security: expect.stringMatching(/failed|warning|passed/),
propUniqueness: expect.stringMatching(/failed|warning|passed/),
multiLangConsistency: expect.stringMatching(/failed|warning|passed/),
linksRelTypeCount: expect.stringMatching(/failed|warning|passed/),
readWriteOnly: expect.stringMatching(/failed|warning|passed/),
uriVariableSecurity: expect.stringMatching(/failed|warning|passed/)
},
detailComments: expect.any(Object)
}
fileNames.forEach( fileName => {
test(fileName, done => {
fs.readFile(path.join(dirPath, fileName), "utf-8", (err, tdToTest) => {
if (err) {done(err)}
tdValidator(tdToTest, ()=>{},{}).then( result => {
expect(result).toEqual(refResult)
const tdJson = JSON.parse(tdToTest)
const typoCount = tdJson['typoCount']
expect(checkTypos(tdToTest).length).toEqual(typoCount)
done()
}, errTwo => {done(errTwo)})
})
})
})
6 changes: 3 additions & 3 deletions packages/web/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,12 @@ export function getExamplesList(docType){
"type": "invalid"
},
"TypoCheckWithoutTypos": {
"addr": "./node_modules/@thing-description-playground/core/examples/tds/valid/typoCheckWithoutTypos.json",
"addr": "./node_modules/@thing-description-playground/core/examples/tds/typo/typoCheckWithoutTypos.json",
"type": "valid"
},
"TypoCheckWithTypos": {
"addr": "./node_modules/@thing-description-playground/core/examples/tds/invalid/typoCheckWithTypos.json",
"type": "warning"
"addr": "./node_modules/@thing-description-playground/core/examples/tds/typo/typoCheckWithTypos.json",
"type": "valid"
}
}
: {
Expand Down

0 comments on commit e93b007

Please sign in to comment.