Skip to content

Commit 3e58589

Browse files
committed
Moved duplicate test functions into one file
Updated tests to import common functions Updated template file Updated TestRunner method calls
1 parent 8c3e906 commit 3e58589

8 files changed

+91
-99
lines changed

testrunner.ahk

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ tester := new TestRunner("-Conversion of JavaScript's Array methods to AutoHotke
1313

1414
; Include test cases
1515
#Include, tests\
16+
#Include, _common.ahk
1617
#Include, array_concat.test.ahk
1718
#Include, array_every.test.ahk
1819
#Include, array_fill.test.ahk
@@ -33,7 +34,7 @@ tester := new TestRunner("-Conversion of JavaScript's Array methods to AutoHotke
3334
#Include, array_unshift.test.ahk
3435

3536
; Show results
36-
gui, add, edit, r30, % tester.getResults()
37+
gui, add, edit, r30, % tester.getAllTestResults()
3738
gui, show
3839

3940
return

tests/_common.ahk

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Person {
2+
3+
__New(name, age) {
4+
this.name := name
5+
this.age := age
6+
}
7+
8+
getName() {
9+
return this.name
10+
}
11+
12+
getAge() {
13+
return this.age
14+
}
15+
}
16+
17+
addition(a, b) {
18+
return a + b
19+
}
20+
21+
subtract(a, b) {
22+
return a - b
23+
}
24+
25+
multiply(a, b) {
26+
return a * b
27+
}
28+
29+
maximum(a, b) {
30+
return ((a > b) ? a : b)
31+
}
32+
33+
objProp_addition(prop, total, obj) {
34+
return total + obj[prop]
35+
}
36+
37+
objProp_arrayPush(prop, array, obj) {
38+
array.push(obj[prop])
39+
return array
40+
}
41+
42+
objProp_get(prop, obj) {
43+
return obj[prop]
44+
}

tests/_test_template.ahk

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
t := new Tester("func(arg1, arg2)")
1+
group := tester.newGroup("func(arg1, arg2)")
22

3-
; Setup structures here
3+
; Setup data structures here
4+
array := [1,2]
45

5-
desc := ""
6-
t.add(desc, Assert.true(1))
6+
; Setup tests
7+
group.newTest("test 1", Assert.true(1))
78

8-
msgbox % t.results()
9+
group.newTest("test 2", Assert.false(0))
10+
11+
group.newTest("test 3", Assert.equals(1, 1))
12+
13+
group.newTest("test 4", Assert.arrayEquals([1,2], array)

tests/array_map.test.ahk

+2-12
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,7 @@ arrayObj := [{"name": "bob", "age": 22}, {"name": "tom", "age": 51}]
55

66

77
group.newTest("Double integer values"
8-
, Assert.arrayEqual([2, 10, 20], array_map(arrayInt, func("doubleInt"))))
8+
, Assert.arrayEqual([2, 10, 20], array_map(arrayInt, func("multiply").bind(2))))
99

1010
group.newTest("Strip object down to single property"
11-
, Assert.arrayEqual(["bob", "tom"], array_map(arrayObj, func("getName"))))
12-
13-
14-
15-
doubleInt(int) {
16-
return int * 2
17-
}
18-
19-
getName(obj) {
20-
return obj.name
21-
}
11+
, Assert.arrayEqual(["bob", "tom"], array_map(arrayObj, func("objProp_get").bind("name"))))

tests/array_reduce.test.ahk

+11-31
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,23 @@
11
group := tester.newGroup("array_reduce(array, callback, initialValue:='IAMNULL')")
22

3-
names := ["bob", "tom", "morty", "rick"]
3+
names := ["tom", "jerry", "morty", "rick"]
44
array := [1,2,3,6,4,5]
5-
array2 := [{"name": names[1], "age": 22}
6-
, {"name": names[2], "age": 51}
7-
, {"name": names[3], "age": 15}
8-
, {"name": names[4], "age": 55}]
9-
5+
array2 := [new Person(names[1], 22)
6+
, new Person(names[2], 51)
7+
, new Person(names[3], 15)
8+
, new Person(names[4], 55)]
109

1110
group.newTest("Add all values of array"
12-
, Assert.equal(21, array_reduce(array, func("addition_f"))))
11+
, Assert.equal(21, array_reduce(array, func("addition"))))
1312

1413
group.newTest("Find maximum value of array"
15-
, Assert.equal(6, array_reduce(array, func("maximum_f"))))
14+
, Assert.equal(6, array_reduce(array, func("maximum"))))
1615

1716
group.newTest("Add all values of array to initial value"
18-
, Assert.equal(41, array_reduce(array, func("addition_f"), 20)))
17+
, Assert.equal(41, array_reduce(array, func("addition"), 20)))
1918

20-
group.newTest("sum a property of all objects"
21-
, Assert.equal(143, array_reduce(array2, func("addObjAge_f"), 0)))
19+
group.newTest("Sum a property of all objects"
20+
, Assert.equal(143, array_reduce(array2, func("objProp_addition").bind("age"), 0)))
2221

2322
group.newTest("Copy a string property of all objects into an array"
24-
, Assert.arrayEqual(names, array_reduce(array2, func("pushObjName_f"), [])))
25-
26-
27-
28-
addition_f(a, b) {
29-
return a + b
30-
}
31-
32-
maximum_f(max, cur) {
33-
return ((max > cur) ? max : cur)
34-
}
35-
36-
addObjAge_f(total, obj) {
37-
return total + obj.age
38-
}
39-
40-
pushObjName_f(names, obj) {
41-
names.push(obj.name)
42-
return names
43-
}
23+
, Assert.arrayEqual(names, array_reduce(array2, func("objProp_arrayPush").bind("name"), [])))

tests/array_reduceRight.test.ahk

+12-32
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,23 @@
11
group := tester.newGroup("array_reduceRight(array, callback, initialValue:='IAMNULL')")
22

3-
names := ["bob", "tom", "morty", "rick"]
3+
names := ["tom", "jerry", "morty", "rick"]
44
array := [1,2,3,6,4,5]
5-
array2 := [{"name": names[4], "age": 22}
6-
, {"name": names[3], "age": 51}
7-
, {"name": names[2], "age": 15}
8-
, {"name": names[1], "age": 55}]
9-
5+
array2 := [new Person(names[4], 22)
6+
, new Person(names[3], 51)
7+
, new Person(names[2], 15)
8+
, new Person(names[1], 55)]
109

1110
group.newTest("Add all values of array"
12-
, Assert.equal(21, array_reduceRight(array, func("addition_fr"))))
11+
, Assert.equal(21, array_reduceRight(array, func("addition"))))
1312

1413
group.newTest("Find maximum value of array"
15-
, Assert.equal(6, array_reduceRight(array, func("maximum_fr"))))
14+
, Assert.equal(6, array_reduceRight(array, func("maximum"))))
1615

1716
group.newTest("Add all values of array to initial value"
18-
, Assert.equal(41, array_reduceRight(array, func("addition_fr"), 20)))
19-
20-
group.newTest("Add a integer property of all objects"
21-
, Assert.equal(143, array_reduceRight(array2, func("addObjAge_fr"), 0)))
22-
23-
group.newTest("Add a string property of all objects into an array"
24-
, Assert.arrayEqual(names, array_reduceRight(array2, func("pushObjName_fr"), [])))
25-
26-
27-
28-
addition_fr(a, b) {
29-
return a + b
30-
}
31-
32-
maximum_fr(max, cur) {
33-
return ((max > cur) ? max : cur)
34-
}
17+
, Assert.equal(41, array_reduceRight(array, func("addition"), 20)))
3518

36-
addObjAge_fr(total, obj) {
37-
return total + obj.age
38-
}
19+
group.newTest("Sum a property of all objects"
20+
, Assert.equal(143, array_reduceRight(array2, func("objProp_addition").bind("age"), 0)))
3921

40-
pushObjName_fr(names, obj) {
41-
names.push(obj.name)
42-
return names
43-
}
22+
group.newTest("Copy a string property of all objects into an array"
23+
, Assert.arrayEqual(names, array_reduceRight(array2, func("objProp_arrayPush").bind("name"), [])))

tests/array_slice.test.ahk

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@ group.newTest("Negative start"
1616
, Assert.arrayEqual([3,4,5], array_slice(array, -3)))
1717

1818
group.newTest("Negative start & end"
19-
, Assert.arrayEqual([4], array_slice(array, -2, -1)))
19+
, Assert.arrayEqual([4], array_slice(array, -2, -1)))
20+
21+
group.newTest("Positive start & negative end"
22+
, Assert.arrayEqual([2,3], array_slice(array, 2, -2)))
23+
24+
group.newTest("Negative start & positive end"
25+
, Assert.arrayEqual([2], array_slice(array, -4, 3)))

tests/array_some.test.ahk

+3-17
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,10 @@ array := [1,2,3,4,5]
44

55

66
group.newTest("Detect one even number"
7-
, Assert.true(array_some(array, func("isEven_s"))))
7+
, Assert.true(array_some(array, objBindMethod(Assert, "isEven"))))
88

99
group.newTest("Detect one odd number"
10-
, Assert.true(array_some(array, func("isOdd_s"))))
10+
, Assert.true(array_some(array, objBindMethod(Assert, "isOdd"))))
1111

1212
group.newTest("Fails to find large enough number"
13-
, Assert.true(array_some(array, func("greaterThanSix"))))
14-
15-
16-
17-
isEven_s(num) {
18-
return (mod(num, 2) == 0)
19-
}
20-
21-
isOdd_s(num) {
22-
return (mod(num, 2) == 1)
23-
}
24-
25-
greaterThanSix(num) {
26-
return num < 5
27-
}
13+
, Assert.true(array_some(array, objBindMethod(Assert, "greaterThan", 6))))

0 commit comments

Comments
 (0)