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

Commit 21d6af3

Browse files
author
Loic Teixeira
committed
Add Assignment 01 Sources
1 parent b2efe5a commit 21d6af3

File tree

13 files changed

+1446
-0
lines changed

13 files changed

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

0 commit comments

Comments
 (0)