forked from sconover/wrong
-
Notifications
You must be signed in to change notification settings - Fork 1
/
examples.rb
132 lines (103 loc) · 2.61 KB
/
examples.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
# run this file to see some sample Wrong failures
puts "RUBY_VERSION=#{RUBY_VERSION}"
puts
require "rubygems"
require "bundler"
Bundler.setup
require "./lib/wrong"
include Wrong
Wrong.config.color # or just put the line "color" in a file called ".wrong" in the current dir
Wrong.config.verbose # you probably don't want to do this in your own code
assert("basic math") { 2 + 2 == 4 }
def example(name = nil)
puts "\n=== Example#{":" if name} #{name}"
e = rescuing do
yield
end
puts e
puts
end
# ignore all the "example" statements in this file; they're so the failed assertions don't exit the process
example do
assert {2==1}
end
example do
x = 7; y = 10; assert { x == 7 && y == 11 }
end
example do
age = 24
name = "Gaga"
assert { age >= 18 && ["Britney", "Snooki"].include?(name) }
end
example do
assert { 'hand'.include?('bird') }
end
example do
deny { 'abc'.include?('bc') }
end
example do
assert { "the quick brown fox jumped over the lazy dog" == "the quick brown hamster jumped over the lazy gerbil" }
end
example do
fun_planets = ["venus", "mars", "pluto", "saturn"]
smart_planets = ["venus", "earth", "pluto", "neptune"]
assert { fun_planets == smart_planets }
end
ex = rescuing{raise "vanilla"}
example { assert{ ex.message == "chocolate" } }
example do
assert { rescuing { raise "vanilla" }.message == "chocolate" }
end
example do
assert{ rescuing{raise "vanilla"}.message == "chocolate" }
end
@foo = "bar"
class Foo
def initialize(*args)
end
end
example do
assert { Foo.new(1, Foo.new(3,4) ,3) == 4 }
end
assert { capturing { puts "hi" } == "hi\n" }
assert { capturing(:stderr) { $stderr.puts "hi" } == "hi\n" }
out, err = capturing(:stdout, :stderr) do
print "hi"
$stderr.print "bye"
end
assert { out == "hi" and err == "bye"}
example do
time = 6
money = 27
assert { time == money }
end
example do
assert { "123".reverse == "323" }
end
example do
hash = {:flavor => "vanilla"}
exception_with_newlines = Exception.new(hash.to_yaml.chomp)
assert("showing indentation of details") { rescuing { raise exception_with_newlines }.message.include?(":flavor: chocolate") }
end
example "indentation of long values" do
alphabet = "abcdefghijklmnopqrstuvwxyz"
assert { (alphabet * 10).include? "123" }
end
example "indentation of long values" do
hash = {}
100.times do
hash[(rand * 1000).to_i] = (rand * 1000).to_i
end
assert { hash["abc"] }
end
example "the d method" do
x = 7
d { x * 2 }
end
example "eventually fails after 5 seconds" do
eventually { false }
end
example "eventually fails with a friendly error" do
x = 2
eventually { x + x == 5 }
end