-
Notifications
You must be signed in to change notification settings - Fork 0
/
rw_ize.coffee
167 lines (125 loc) · 3.74 KB
/
rw_ize.coffee
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
rw = require "rw_ize"
assert = require "assert"
describe "rw_ize", () ->
describe "rw(k, v)", () ->
it "sets value of key", () ->
obj = {}
rw.ize(obj)
obj.read_able "name"
obj.rw "name", "ted"
assert.equal obj.name(), "ted"
it "throws error if called w/o args.", () ->
obj = {}
rw.ize(obj)
obj.read_able "name"
obj.rw "name", "ted"
err = null
try
obj.rw()
catch e
err = e
assert.deepEqual err.message, "Key not found: undefined"
it "raise an error if trying to set an unknown key", () ->
obj = {}
rw.ize(obj)
obj.read_able "name"
err = try
obj.rw("names", "ted")
catch e
e
assert.deepEqual err.message, "Key not found: names"
describe "read_able", () ->
it "adds a method to the prototype", () ->
class Car
rw.ize(this)
@read_able "year"
constructor: () ->
@rw 'year', "1999"
c = new Car()
assert.equal c.year(), "1999"
it "adds a method to objects", () ->
car = {}
rw.ize(car)
car.read_able "price"
car.rw 'price', "$10"
assert.equal car.price(), "$10"
describe "write_able", () ->
it "adds a method to the prototype", () ->
class Spaceship
rw.ize(this)
@write_able "location"
constructor: () ->
s = new Spaceship()
s.location "NYC"
assert.equal s.rw('location'), "NYC"
describe "read_write_able", () ->
it "adds both a read and write method to the prototype", () ->
class Unicycle
rw.ize(this)
@read_write_able "price"
constructor: () ->
@rw 'price', "$7"
u = new Unicycle()
assert.equal u.price(), "$7"
u.price "$8"
assert.equal u.price(), "$8"
describe "read_able_bool", () ->
it "adds a read method to the prototype", () ->
class Truck
rw.ize(this)
@read_able_bool "on"
constructor: () ->
t = new Truck()
assert.equal t.on(), false
it "reads from manually set value", () ->
class Truck
rw.ize(this)
@read_able_bool "on"
constructor: () ->
@rw "on", true
t = new Truck()
assert.equal t.on(), true
describe "read_write_able_bool", () ->
it "adds both a read and write method to the prototype", () ->
class Switch
rw.ize(this)
@read_write_able_bool "on"
constructor: () ->
s = new Switch()
assert.equal s.on(), false
s.on(true)
assert.equal s.on(), true
it "adds both a read and write method to the object", () ->
ship = {}
rw.ize(ship)
ship.read_write_able_bool "afloat"
assert.equal ship.afloat(), false
ship.afloat(true)
assert.equal ship.afloat(), true
it "adds a seperate read/write method for each different bool", () ->
ship = {}
rw.ize(ship)
ship.read_write_able_bool "afloat"
ship.read_write_able_bool "sinking"
ship.afloat(true)
assert.equal ship.afloat(), true
assert.equal ship.sinking(), false
it "reads from manually set value", () ->
class Sailboat
rw.ize(this)
@read_write_able_bool "engine_on"
constructor: () ->
@rw "engine_on", true
t = new Sailboat()
assert.equal t.engine_on(), true
describe "private_ize", () ->
it "throws an error if read method is used as a writer", () ->
ship = {}
rw.ize(ship)
ship.private_ize "color"
ship.read_able "color"
try
ship.color "red"
catch e
err = e
assert.equal err.message, "Key is not write_able: color"