/
tc_convention
78 lines (75 loc) · 2.36 KB
/
tc_convention
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
#! ruby
require 'test/unit'
require 'such/convention'
class TestConvention < Test::Unit::TestCase
GOOD_CONFIG = {
# lower
abc: {any:'Hash',values:[1,2,3]},
# CAPS
ABC: ['Any',:item,{array:1}],
# Camel
String: 'One',
Float: 2.0,
Integer: 3,
Symbol: :four,
# CAPS!
ABC!: [{just: 'hash'},['array',:or],'string'],
# lower!
abc!: [:bang, 'String', {do: 'thing'}, [1,2,3]],
# CAPS!
XYZ!: ['String', {do: 'thing'}, [1,2,3]],
# Camel!
Abc!: 'any items',
Xyz!: {any: ['items']},
Really!: ['any', {items:'whatsoever'}],
# lower?
true?: true,
false?: false,
# CAPS?
TRUE?: true,
FALSE?: false,
NIL?: nil,
# Camel?
Ok?: 'Yes',
NotOk?: nil,
}
def test_good_config
errors = Such::Convention.validate GOOD_CONFIG
assert_nil errors
end
BAD_CONFIG = {
'String' => 'Do not use String keys',
'not valid': 'Key must be /^\w+[!?]?$/',
abc: 'lower should be hash',
xyz: {x: 'ok', z: ['also','ok'], y: /not ok/},
ABC: 'CAPS should be array',
XYZ: ['Must be valid items, not', /regexp/],
Abc: ['Must', 'be', 'item'],
Xyz: {must: 'be', an: 'item'},
Item: /still not item/,
abc!: 'Must be item bang array',
ABC!: [:must,:be,:explicit,'array'],
Abc!: /any valid items/,
abc?: nil, # Must be true or false
ABC?: 'Must be true, false, or nil',
Abc?: false, # Must be item or nil
}
def test_bad_config
errors = Such::Convention.validate BAD_CONFIG
assert_equal 'unrecognized item symbol', errors['String']
assert_equal 'unrecognized item symbol', errors[:'not valid']
assert_equal 'unrecognized item hash', errors[:abc]
assert_equal 'unrecognized item hash', errors[:xyz]
assert_equal 'unrecognized item array', errors[:ABC]
assert_equal 'unrecognized item array', errors[:XYZ]
assert_equal 'unrecognized item tangible', errors[:Abc]
assert_equal 'unrecognized item tangible', errors[:Xyz]
assert_equal 'unrecognized item tangible', errors[:Item]
assert_equal 'unrecognized bang array', errors[:abc!]
assert_equal 'unrecognized explicit array', errors[:ABC!]
assert_equal 'unrecognized items', errors[:Abc!]
assert_equal 'unrecognized boolean', errors[:abc?]
assert_equal 'unrecognized boolean|nil', errors[:ABC?]
assert_equal 'unrecognized item value|nil', errors[:Abc?]
end
end