-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_test.rb
174 lines (164 loc) · 4.17 KB
/
class_test.rb
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
167
168
169
170
171
172
173
174
require "test_helper"
class ClassTest < Minitest::Test
def test_empty_class_with_no_description
want = <<~RUBY.strip
class Example
end
RUBY
have = Ginny::Class.create(name: "Example").render()
assert_equal_and_print(want, have)
end
def test_empty_class_with_description
want = <<~RUBY.strip
# This is just an example.
class Example
end
RUBY
have = Ginny::Class.create(
name: "Example",
description: "This is just an example.",
).render()
assert_equal(want, have)
end
def test_empty_class_with_parent
want = <<~RUBY.strip
# This example is object-oriented.
class Example < Parent
end
RUBY
have = Ginny::Class.create(
name: "Example",
description: "This example is object-oriented.",
parent: "Parent",
).render()
assert_equal(want, have)
end
def test_class_with_attributes
want = <<~RUBY.strip
# This class models a person.
class Human < Mammal
# @return [String]
attr_accessor :name
# Number of years the human has been alive.
# @return [Integer]
attr_accessor :age
end
RUBY
have = Ginny::Class.create(
name: "Human",
description: "This class models a person.",
parent: "Mammal",
attrs: [
{ name: "name", type: "String" },
{ name: "age", description: "Number of years the human has been alive.", type: "Integer" },
],
).render()
assert_equal(want.strip, have.strip)
end
def test_class_with_in_module
want = <<~RUBY.strip
module MilkyWay
module Earth
# This class models a person.
class Human < Mammal
# @return [String]
attr_accessor :name
# Number of years the human has been alive.
# @return [Integer]
attr_reader :age
end
end
end
RUBY
have = Ginny::Class.create(Ginny.load_file(file_fixture("in/person2.yml"))).render()
assert_equal(want.strip, have.strip)
end
def test_class_with_body
want = <<~RUBY.strip
module C4
module Elements
# - Id: 112
# - Name: Pier Name
# - Type: AN
# - Min/Max: 2/14
# - Description: Free-form name of the pier.
class PierName < C4::Element::AN
# @return [void]
def initialize()
@id = 112
@name = "Pier Name"
@type = "AN"
self.min = 2
self.max = 14
end
end
end
end
RUBY
have = Ginny::Class.create(
name: "PierName",
parent: "C4::Element::AN",
modules: ["C4", "Elements"],
description: <<~STRING.strip,
- Id: 112
- Name: Pier Name
- Type: AN
- Min/Max: 2/14
- Description: Free-form name of the pier.
STRING
body: <<~RUBY.strip,
# @return [void]
def initialize()
@id = 112
@name = "Pier Name"
@type = "AN"
self.min = 2
self.max = 14
end
RUBY
).render()
assert_equal(want.strip, have.strip)
end
def test_preserving_newlines
want = <<~RUBY.strip
module Eddy
module Elements
class SecurityInformationQualifier < Eddy::Element::ID
# @return [void]
def initialize()
@id = "I03"
end
# @return [Array<String>]
def code_list()
return [
"00",
"01"
]
end
end
end
end
RUBY
constructor = Ginny::Func.create({
name: "initialize",
body: '@id = "I03"',
}).render()
code_list = Ginny::Func.create({
name: "code_list",
return_type: "Array<String>",
body: <<~FUNC_BODY,
return [
"00",
"01"
]
FUNC_BODY
}).render()
have = Ginny::Class.create({
name: "SecurityInformationQualifier",
parent: "Eddy::Element::ID",
modules: ["Eddy", "Elements"],
body: constructor + "\n\n" + code_list,
}).render()
assert_equal(want, have)
end
end