-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
patch.test.js
executable file
·91 lines (81 loc) · 2.46 KB
/
patch.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#! /usr/local/bin/node --use_strict
const tap = require("tap")
const { Patch } = require("./patch.js")
const { rowDelimiter, columnDelimiter } = new Patch()
const encodedRowDelimiter = encodeURIComponent(rowDelimiter)
const encodedColumnDelimiter = encodeURIComponent(columnDelimiter)
const runTree = (testTree) =>
Object.keys(testTree).forEach((key) => testTree[key](tap.same))
const testTree = {}
const encodeString = (str) =>
str.replace(/\&/g, rowDelimiter).replace(/\=/g, columnDelimiter)
const tests = [
{
string: encodeString(`foo=bar`),
object: { foo: "bar" },
array: [["foo", "bar"]],
},
{ string: "", object: {}, array: [[""]] },
{
string: encodeString(`Country+Name=United+States`),
object: { "Country Name": "United States" },
array: [["Country Name", "United States"]],
},
{
string: encodeString(`countries=United+States=Germany&chart=Map`),
object: {
countries: ["United States", "Germany"],
chart: "Map",
},
array: [
["countries", "United States", "Germany"],
["chart", "Map"],
],
},
{
string: `group=HighIncome=Canada=Norway&group=MediumIncome=Spain=Greece`,
array: [
["group", "HighIncome", "Canada", "Norway"],
["group", "MediumIncome", "Spain", "Greece"],
],
},
{
string: `filters=&=time=lastMonth`,
array: [
[`filters`, ""],
["", `time`, `lastMonth`],
],
object: {
filters: "",
"": [`time`, `lastMonth`],
},
},
{
string: `paragraph${columnDelimiter}${encodedRowDelimiter}${encodedColumnDelimiter}`,
object: {
paragraph: `${rowDelimiter}${columnDelimiter}`,
},
array: [["paragraph", `${rowDelimiter}${columnDelimiter}`]],
},
]
testTree.basics = async (areEqual) => {
areEqual(new Patch().object, {})
tests.forEach((test) => {
if (test.object) {
areEqual(new Patch(test.object).uriEncodedString, test.string)
areEqual(new Patch(test.object).array, test.array)
areEqual(new Patch(test.string).object, test.object)
areEqual(new Patch(test.array).object, test.object)
}
areEqual(new Patch(test.string).array, test.array)
areEqual(new Patch(test.array).uriEncodedString, test.string)
})
}
testTree.devilTestsCase = async (areEqual) => {
const original = {
title: "!*'();:@&=+$,/?#[]-_.~|\"\\",
}
areEqual(new Patch(new Patch(original).uriEncodedString).object, original)
}
if (module && !module.parent) runTree(testTree)
module.exports = { testTree }