Skip to content
This repository was archived by the owner on Jan 5, 2021. It is now read-only.

Commit 83c0cab

Browse files
author
Loic Teixeira
committed
Add Assignment 05 Sources
1 parent 1198561 commit 83c0cab

File tree

11 files changed

+888
-0
lines changed

11 files changed

+888
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class BasicObject
2+
NIX = ::Object.new
3+
4+
def nix
5+
return NIX
6+
end
7+
8+
def nix?
9+
return equal?(NIX)
10+
end
11+
12+
def checkcast(s)
13+
return self
14+
end
15+
end
16+
17+
class Class
18+
alias_method :__new__, :new
19+
20+
def nil
21+
return nil
22+
end
23+
end
24+
25+
class << self
26+
def each_manifest_name(dir)
27+
path = File.join(dir, "manifest.txt")
28+
lines = File.readlines(path)
29+
lines.each do |line|
30+
name = line.strip
31+
unless name.empty?
32+
yield name
33+
end
34+
end
35+
return
36+
end
37+
38+
def load_manifest(dir)
39+
each_manifest_name(dir) do |name|
40+
path = File.join(dir, "ruby", name)
41+
if File.exist?(path)
42+
load(path)
43+
end
44+
end
45+
return
46+
end
47+
48+
def load_parent_manifest(dir)
49+
load_manifest(File.dirname(dir))
50+
return
51+
end
52+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Integer
2+
def hash
3+
return self
4+
end
5+
end
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
dir = File.dirname(File.expand_path(__FILE__))
2+
3+
require(File.join(dir, "base.rb"))
4+
5+
class Memory
6+
class Fault < Exception
7+
end
8+
9+
class << self
10+
def new(c)
11+
if c < 0
12+
raise Fault.new("negative capacity: #{c}")
13+
end
14+
values = Array.new(c)
15+
return Memory.__new__(values)
16+
end
17+
18+
def [](*values)
19+
return Memory.__new__(values)
20+
end
21+
end
22+
23+
def initialize(values)
24+
super()
25+
@values = values
26+
return
27+
end
28+
29+
def __assert_bounds(i)
30+
if i < 0 || i >= capacity
31+
raise Fault.new("index out of bounds for capacity #{capacity}: #{i}")
32+
end
33+
return
34+
end
35+
36+
def __get(i)
37+
return @values[i]
38+
end
39+
40+
def eql?(o)
41+
if o.is_a?(Memory)
42+
if capacity == o.capacity
43+
capacity.times do |i|
44+
unless @values[i].eql?(o.__get(i))
45+
return false
46+
end
47+
end
48+
return true
49+
end
50+
end
51+
return false
52+
end
53+
54+
def capacity
55+
return @values.size
56+
end
57+
58+
def [](i)
59+
__assert_bounds(i)
60+
return @values[i]
61+
end
62+
63+
def []=(i, e)
64+
__assert_bounds(i)
65+
@values[i] = e
66+
return
67+
end
68+
end
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
dir = File.dirname(File.expand_path(__FILE__))
2+
3+
require(File.join(dir, "base.rb"))
4+
5+
module QuickSpec
6+
@status = 0
7+
8+
class << self
9+
attr_accessor :status
10+
end
11+
12+
module Expectations
13+
class ExpectationNotMetError < Exception
14+
end
15+
16+
class ExpectationTarget
17+
def initialize(observed)
18+
super()
19+
@observed = observed
20+
return
21+
end
22+
23+
def to(matcher)
24+
matcher.to(@observed)
25+
return
26+
end
27+
28+
def not_to(matcher)
29+
matcher.not_to(@observed)
30+
return
31+
end
32+
end
33+
end
34+
35+
module Matchers
36+
module BuiltIn
37+
class YieldProbe
38+
def initialize
39+
super()
40+
@yielded_args = []
41+
return
42+
end
43+
44+
attr_reader :yielded_args
45+
46+
def call(args)
47+
if args.size == 1
48+
@yielded_args.push(args.first)
49+
else
50+
@yielded_args.push(args)
51+
end
52+
return
53+
end
54+
55+
def to_proc
56+
return lambda { |*args| call(args) }
57+
end
58+
end
59+
60+
class BaseMatcher
61+
def initialize(expected)
62+
super()
63+
@expected = expected
64+
return
65+
end
66+
end
67+
68+
class Eql < BaseMatcher
69+
def to(observed)
70+
unless observed.eql?(@expected)
71+
message = "expected #{observed.inspect} to eql #{@expected.inspect}"
72+
raise Expectations::ExpectationNotMetError.new(message)
73+
end
74+
return
75+
end
76+
77+
def not_to(observed)
78+
if observed.eql?(@expected)
79+
message = "expected #{observed.inspect} not to eql #{@expected.inspect}"
80+
raise Expectations::ExpectationNotMetError.new(message)
81+
end
82+
return
83+
end
84+
end
85+
86+
class RaiseError < BaseMatcher
87+
def to(observed)
88+
begin
89+
observed.call
90+
rescue @expected
91+
return
92+
end
93+
message = "expected to raise #{@expected.inspect}"
94+
raise Expectations::ExpectationNotMetError.new(message)
95+
end
96+
97+
def not_to(observed)
98+
begin
99+
observed.call
100+
rescue @expected
101+
message = "expected not to raise #{@expected.inspect}"
102+
raise Expectations::ExpectationNotMetError.new(message)
103+
end
104+
return
105+
end
106+
end
107+
108+
class YieldControl < BaseMatcher
109+
def to(observed)
110+
probe = YieldProbe.new
111+
observed.call(probe)
112+
if probe.yielded_args.empty?
113+
message = "expected to yield"
114+
raise Expectations::ExpectationNotMetError.new(message)
115+
end
116+
return
117+
end
118+
119+
def not_to(observed)
120+
probe = YieldProbe.new
121+
observed.call(probe)
122+
unless probe.yielded_args.empty?
123+
message = "expected not to yield but yielded #{probe.yielded_args.inspect}"
124+
raise Expectations::ExpectationNotMetError.new(message)
125+
end
126+
return
127+
end
128+
end
129+
130+
class YieldSuccessiveArgs < BaseMatcher
131+
def to(observed)
132+
probe = YieldProbe.new
133+
observed.call(probe)
134+
unless probe.yielded_args.eql?(@expected)
135+
message = "expected to yield #{@expected.inspect} but yielded #{probe.yielded_args.inspect}"
136+
raise Expectations::ExpectationNotMetError.new(message)
137+
end
138+
return
139+
end
140+
141+
def not_to(observed)
142+
probe = YieldProbe.new
143+
observed.call(probe)
144+
if probe.yielded_args.eql?(@expected)
145+
message = "expected not to yield #{@expected.inspect} but yielded #{probe.yielded_args.inspect}"
146+
raise Expectations::ExpectationNotMetError.new(message)
147+
end
148+
return
149+
end
150+
end
151+
end
152+
153+
def expect(observed = nix, &block)
154+
if block_given?
155+
unless observed.nix?
156+
raise ArgumentError.new
157+
end
158+
return Expectations::ExpectationTarget.new(block)
159+
else
160+
if observed.nix?
161+
raise ArgumentError.new
162+
end
163+
return Expectations::ExpectationTarget.new(observed)
164+
end
165+
end
166+
167+
def eql(expected)
168+
return BuiltIn::Eql.new(expected)
169+
end
170+
171+
def raise_error(expected)
172+
return BuiltIn::RaiseError.new(expected)
173+
end
174+
175+
def yield_control
176+
return BuiltIn::YieldControl.new(nil)
177+
end
178+
179+
def yield_successive_args(*args)
180+
return BuiltIn::YieldSuccessiveArgs.new(args)
181+
end
182+
end
183+
184+
module Core
185+
class << self
186+
def puts_exception_message(e)
187+
if e.is_a?(Expectations::ExpectationNotMetError)
188+
puts <<-END
189+
#{e.message}
190+
END
191+
else
192+
puts <<-END
193+
#{e.class}: #{e.message}
194+
END
195+
end
196+
return
197+
end
198+
199+
def puts_exception_backtrace(e)
200+
first = true
201+
e.backtrace.each do |line|
202+
if line.start_with?("#{__FILE__}:")
203+
unless first
204+
break
205+
end
206+
else
207+
first = false
208+
puts <<-END
209+
#{line}
210+
END
211+
end
212+
end
213+
return
214+
end
215+
216+
def puts_exception(e)
217+
puts_exception_message(e)
218+
puts_exception_backtrace(e)
219+
return
220+
end
221+
end
222+
223+
class ExampleGroup
224+
include Matchers
225+
226+
class << self
227+
def it(message, &block)
228+
print " it #{message}... "
229+
begin
230+
new.instance_eval(&block)
231+
rescue Exception
232+
puts "fail"
233+
Core.puts_exception($!)
234+
QuickSpec.status = 1
235+
return
236+
end
237+
puts "ok"
238+
return
239+
end
240+
end
241+
end
242+
end
243+
244+
class << self
245+
def describe(name, &block)
246+
puts
247+
puts <<-END
248+
describe #{name}:
249+
END
250+
c = Class.new(Core::ExampleGroup)
251+
c.class_eval(&block)
252+
return
253+
end
254+
end
255+
end
256+
257+
class << self
258+
def describe(name, &block)
259+
QuickSpec.describe(name, &block)
260+
return
261+
end
262+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
dir = File.dirname(File.expand_path(__FILE__))
2+
3+
require(File.join(dir, "base.rb"))
4+
require(File.join(dir, "integer.rb"))
5+
require(File.join(dir, "memory.rb"))
6+
7+
load_parent_manifest(dir)

0 commit comments

Comments
 (0)