forked from dojo-toulouse/elixir-koans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
about_strings.exs
150 lines (122 loc) · 3.79 KB
/
about_strings.exs
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
#!/usr/bin/env elixir
ExUnit.start
defmodule About_Strings do
use ExUnit.Case
use Koans
think "creating a new string is easy" do
a_string = "hello world!"
assert a_string == __?
end
think "how long is a piece of string?" do
a_string = "Hello there"
assert String.length(a_string) == __?
end
think "what does the string start with?" do
a_string = "Hello there"
assert String.starts_with?(a_string, "H") == __?
assert String.starts_with?(a_string, ["Bonjour", "Hello"]) == __?
assert String.starts_with?(a_string, ["Bonjour", "Greetings"]) == __?
end
think "what does the string end with?" do
a_string = "Live long and prosper"
assert String.ends_with?(a_string, "prosper") == __?
assert String.ends_with?(a_string, ["multiply", "prosper"]) == __?
assert String.ends_with?(a_string, ["keep calm"]) == __?
end
think "does a string contain something?" do
a_string = "May the force be with you"
assert String.contains?(a_string, "force") == __?
assert String.contains?(a_string, ["voyager", "you"]) == __?
assert String.contains?(a_string, ["prosper"]) == __?
end
think "accessing letters by their positions" do
a_string = "Hello world!"
assert String.at(a_string, 2) == __?
assert String.at(a_string, 20) == __?
end
think "slicing a string" do
a_string = "Hello world!"
assert String.slice(a_string, 6, 5) == __?
assert String.slice(a_string, -3, 6) == __?
assert String.slice(a_string, 20, 5) == __?
assert String.slice(a_string, 4, 0) == __?
assert String.slice(a_string, 0..5) == __?
end
think "capitalization" do
a_string = "hello world!"
assert String.capitalize(a_string) == __?
end
think "upcase" do
a_string = "hello world!"
assert String.upcase(a_string) == __?
end
think "downcase" do
a_string = "SPEAK QUIETLY"
assert String.downcase(a_string) == __?
end
think "reversing a string" do
a_string = "sdrow sdrawkcab"
assert String.reverse(a_string) == __?
end
think "say it again" do
a_string = "repeat this"
assert String.duplicate(a_string, 3) == __?
end
think "stripping on the left" do
a_string = " abc "
assert String.lstrip(a_string) == __?
end
think "stripping on the left with specific characters" do
a_string = "$ abc $"
assert String.lstrip(a_string, ?$) == __?
end
think "stripping on the right" do
a_string = " abc "
assert String.rstrip(a_string) == __?
end
think "stripping on the right" do
a_string = " abc $"
assert String.rstrip(a_string, ?$) == __?
end
think "stripping on both sides" do
a_string = " abc "
assert String.strip(a_string) == __?
end
think "stripping on both sides with a specific character" do
a_string = "$ abc $"
assert String.strip(a_string, ?$) == __?
end
think "left justification" do
a_string = "3"
assert String.ljust(a_string, 3) == __?
end
think "left justification with a specific character" do
a_string = "3"
assert String.ljust(a_string, 3, ?0) == __?
end
think "right justification" do
a_string = "3"
assert String.rjust(a_string, 3) == __?
end
think "right justification with a specific character" do
a_string = "2"
assert String.rjust(a_string, 3, ?0) == __?
end
think "converting to an atom" do
a_string = "atomized"
assert String.to_atom(a_string) == __?
end
think "converting to an integer" do
a_string = "10"
assert String.to_integer(a_string) == __?
end
think "converting to an integer using a different base" do
a_string = "11"
assert String.to_integer(a_string, 16) == __?
assert String.to_integer(a_string, 2) == __?
end
think "converting to a float" do
a_string = "10.99"
assert String.to_float(a_string) == __?
end
end