/
string.fy
140 lines (118 loc) · 3.87 KB
/
string.fy
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
FancySpec describe: String with: {
it: "is the empty string on initialization" when: {
str = String new
str is: ""
}
it: "is the concatination of the strings" with: '+ when: {
str1 = "hello "
str2 = "world"
str3 = "!"
str1 + str2 + str3 is: "hello world!"
}
it: "concatenates the argument's string value with a string" with: '++ when: {
"I'm " ++ 21 ++ " years old!" is: "I'm 21 years old!"
}
it: "returns the correct substring" with: 'from:to: when: {
"hello, world" from: 2 to: 5 . is: "llo,"
"hello, world"[[2,5]] . is: "llo,"
}
it: "returns the upcased string" with: 'upcase when: {
"hello, world" upcase is: "HELLO, WORLD"
}
it: "returns the downcased string" with: 'downcase when: {
"HELLO, WORLD" downcase is: "hello, world"
}
it: "returns the same string by down- and upcasing in a row" when: {
"HELLO, WORLD" downcase upcase is: "HELLO, WORLD"
}
it: "iterates over each character in a string" with: 'each: when: {
str = "Hello, World!"
i = 0
str each: |char| {
char is: (str at: i)
i = i + 1
}
}
it: "behaves like a collection/sequence via each:" with: 'uniq when: {
str = "Hello, World!"
str uniq join: "" . is: "Helo, Wrd!"
}
it: "has all its characters as instances of String class" with: 'all?: when: {
str = "foo bar baz"
str all?: |c| { c is_a?: String } . is: true
}
it: "drops all characters upto a whitespace" with: 'drop_while: when: {
"hello world" drop_while: |c| { c != " " } . join: "" . is: " world"
}
it: "is empty" with: 'empty? when: {
"" empty? is: true
" " empty? is: false
String new empty? is: true
}
it: "is blank" with: 'blank? when: {
"" blank? is: true
" " blank? is: true
"-" blank? is: false
" " blank? is: true
"hello world" blank? is: false
"hello world" at: 5 . blank? is: true
}
it: "is evaluated as fancy code and returns the correct value" when: {
x = "'foo" eval
x is: 'foo
"3 + 4" eval is: 7
"'foo to_s upcase" eval is: "FOO"
"33.33" eval is: 33.33
}
it: "returns itself times n" with: '* when: {
"foo" * 2 is: "foofoo"
"f" ++ ("o" * 2) ++ "bar" is: "foobar"
}
it: "splits a string at a given seperator string" with: 'split: when: {
str = "hello, world, how are you?"
str split: ", " . is: ["hello", "world", "how are you?"]
"1,2,3,,4,5" split: "," . is: ["1", "2", "3", "", "4", "5"]
",1,2,3,4," split: "," . is: ["", "1", "2", "3", "4"]
"foo bar\n baz yo" split is: ["foo", "bar", "baz", "yo"]
"foo bar\n baz yo" words is: ["foo", "bar", "baz", "yo"]
}
it: "supports basic string interpolation" when: {
"hello, #{10 * 10} world!" is: "hello, 100 world!"
x = "world"
"hello, #{x}!!" is: "hello, world!!"
"hello, #{x}, Fancy #{'rocks to_s upcase}!!" is: "hello, world, Fancy ROCKS!!"
}
it: "returns the String as a Symbol" with: 'to_sym when: {
"foo" to_sym is: 'foo
"foo:bar:" to_sym is: 'foo:bar:
"FooBar?!" to_sym is: 'FooBar?!
"+-&/^\?a!" to_sym is '+-&/^\?a!
}
it: "allows replacing characters in the string" with: '[]: when: {
s = "hello"
s[0]: "H"
s is: "Hello"
s[0]: "Good day. H"
s is: "Good day. Hello"
s[-1]: "o."
s is: "Good day. Hello."
}
it: "contains a substring" with: 'includes?: when: {
"foo bar baz" includes?: "foo" . is: true
"foo bar baz" includes?: "bar" . is: true
"foo bar baz" includes?: "baz" . is: true
"foo bar baz" includes?: " " . is: true
"foo bar baz" includes?: "" . is: true
"foo bar baz" includes?: "foobarbaz" . is: false
}
it: "should remove any leading indentation" with: 'skip_leading_indentation when: {
"""
hello, world!
how are you?
""" skip_leading_indentation is: "hello, world!\nhow are you?"
str = """
foo, bar
"""
str skip_leading_indentation is_not: str
}
}