-
Notifications
You must be signed in to change notification settings - Fork 330
/
Copy pathFormatting.scala
65 lines (52 loc) · 1.75 KB
/
Formatting.scala
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
/*
* scala-exercises - exercises-stdlib
* Copyright (C) 2015-2016 47 Degrees, LLC. <http://www.47deg.com>
*/
package stdlib
import org.scalatest._
/** @param name formatting
*
*/
object Formatting extends FlatSpec with Matchers with org.scalaexercises.definitions.Section {
/** String can be placed in format:
*/
def placedInFormatFormatting(res0: String) {
val s = "Hello World"
"Application %s".format(s) should be(res0)
}
/** Character Literals can be a single character:
*/
def characterFormatting(res0: String, res1: String) {
val a = 'a'
val b = 'B'
//format(a) is a string format, meaning the "%c".format(x)
//will return the string representation of the char.
"%c".format(a) should be(res0)
"%c".format(b) should be(res1)
}
/** Character Literals can be an escape sequence, including octal or hexidecimal:
*/
def escapeSequenceFormatting(res0: String, res1: String, res2: String, res3: String) {
val c = '\u0061' //unicode for a
val d = '\141' //octal for a; not supported in scala 2.13.0, use '\u0061' instead
val e = '\"' // can also be specified as '"' (there is no need to escape the double quote)
val f = '\\'
"%c".format(c) should be(res0)
"%c".format(d) should be(res1)
"%c".format(e) should be(res2)
"%c".format(f) should be(res3)
}
/** Formatting can also include numbers:
*/
def includingNumbersFormatting(res0: String) {
val j = 190
"%d bottles of beer on the wall" format j - 100 should be(res0)
}
/** Formatting can be used for any number of items, like a string and a number:
*/
def anyNumberOfItemsFormatting(res0: String) {
val j = 190
val k = "vodka"
"%d bottles of %s on the wall".format(j - 100, k) should be(res0)
}
}