1
1
Fork 0

Regenerated samples

This commit is contained in:
Svetlana Isakova 2020-12-11 22:36:39 +01:00
parent 26836b75dc
commit e3150e9d86
34 changed files with 101 additions and 144 deletions

View File

@ -22,7 +22,7 @@ private fun <L, R> runTest(
/**
* Compares the string representation
* of the object with the string `value`.
* of this object with the string `rval`.
*/
infix fun <T : Any> T.eq(rval: String) {
runTest(this, rval) {
@ -31,8 +31,7 @@ infix fun <T : Any> T.eq(rval: String) {
}
/**
* Verifies that this object is
* equal to `value`.
* Verifies this object is equal to `rval`.
*/
infix fun <T> T.eq(rval: T) {
runTest(this, rval) {
@ -41,8 +40,7 @@ infix fun <T> T.eq(rval: T) {
}
/**
* Verifies that this object is not
* equal to `value`.
* Verifies this object is != `rval`.
*/
infix fun <T> T.neq(rval: T) {
runTest(this, rval, checkEquals = false) {
@ -52,7 +50,7 @@ infix fun <T> T.neq(rval: T) {
/**
* Verifies that a `Double` number is equal
* to `value` within a positive delta.
* to `rval` within a positive delta.
*/
infix fun Double.eq(rval: Double) {
runTest(this, rval) {
@ -89,11 +87,9 @@ class CapturedException(
/**
* Captures an exception and produces
* information about it. Usage:
* ```
* capture {
* // Code that fails
* } eq "FailureException: message"
* ```
* capture {
* // Code that fails
* } eq "FailureException: message"
*/
fun capture(f:() -> Unit): CapturedException =
try {
@ -112,12 +108,12 @@ object trace {
}
/**
* Compares trc contents to a multiline
* `String` by ignoring line separators.
* `String` by ignoring white space.
*/
infix fun eq(multiline: String) {
val trace = trc.joinToString("\n")
val expected = multiline.trimIndent()
.trim().replace("\n", " ")
.replace("\n", " ")
runTest(trace, multiline) {
trace.replace("\n", " ") == expected
}

View File

@ -3,14 +3,14 @@ import interoperability.JTool
import atomictest.*
fun main() {
val xn = JTool.get(null) // [1]
xn?.method() eq null // [2]
capture {
xn.method() // [3]
} contains listOf("NullPointerException")
val xn: JTool? = JTool.get(null) // [1]
xn?.method() eq null
val yn: JTool? = JTool.get(null) // [4]
yn?.method() eq null
val yn = JTool.get(null) // [2]
yn?.method() eq null // [3]
capture {
yn.method() // [4]
} contains listOf("NullPointerException")
capture {
val zn: JTool = JTool.get(null) // [5]

View File

@ -8,19 +8,5 @@ fun main() {
val list = listOf(9, 10, 11)
varargs(
"y", 7, 8, *list.toIntArray()) // [3]
trace eq """
1
2
3
4
5
6
x
7
8
9
10
11
y
"""
trace eq "1 2 3 4 5 6 x 7 8 9 10 11 y"
}

View File

@ -11,14 +11,5 @@ fun varargs(s: String, vararg ints: Int) {
fun main() {
varargs("primes", 5, 7, 11, 13, 17, 19, 23)
trace eq """
5
7
11
13
17
19
23
primes
"""
trace eq "5 7 11 13 17 19 23 primes"
}

View File

@ -17,7 +17,6 @@ class Actual(
class Other : Parent {
override val ch: Char // [3]
get() = 'B'
override fun f() = 34 // [4]
}

View File

@ -1,13 +1,13 @@
// Abstract/WithPropertyAccessor.kt
// Abstract/PropertyAccessor.kt
package abstractclasses
import atomictest.eq
interface WithPropertyAccessor {
interface PropertyAccessor {
val a: Int
get() = 11
}
class Impl : WithPropertyAccessor
class Impl : PropertyAccessor
fun main() {
Impl().a eq 11

View File

@ -10,11 +10,11 @@ files:
visible: true
- name: src/Implementations.kt
visible: true
- name: src/WithPropertyAccessor.kt
visible: true
- name: src/NoMultipleInheritance.kt
visible: true
- name: src/MultipleInterfaceInheritance.kt
visible: true
- name: src/InterfaceCollision.kt
visible: true
- name: src/PropertyAccessor.kt
visible: true

View File

@ -16,16 +16,15 @@ class Chimpanzee(weight: Double, age: Int) :
class BonoboB(weight: Double, age: Int) :
Bonobo(weight, age)
fun info(ape: GreatApe) =
"weight: ${ape.weight} age: ${ape.age}"
fun GreatApe.info() = "wt: $weight age: $age"
fun main() {
info(GreatApe(100.0, 12)) eq
"weight: 100.0 age: 12"
info(Bonobo(110.0, 13)) eq
"weight: 110.0 age: 13"
info(Chimpanzee(120.0, 14)) eq
"weight: 120.0 age: 14"
info(BonoboB(130.0, 15)) eq
"weight: 130.0 age: 15"
GreatApe(100.0, 12).info() eq
"wt: 100.0 age: 12"
Bonobo(110.0, 13).info() eq
"wt: 110.0 age: 13"
Chimpanzee(120.0, 14).info() eq
"wt: 120.0 age: 14"
BonoboB(130.0, 15).info() eq
"wt: 130.0 age: 15"
}

View File

@ -12,7 +12,6 @@ open class House(
fullAddress.substringAfter(", ")
.substringBefore(" "),
fullAddress.substringAfterLast(" "))
val fullAddress: String
get() = "$address, $state $zip"
}
@ -26,7 +25,7 @@ class VacationHouse(
): House(address, state, zip) {
override fun toString() =
"Vacation house at $fullAddress " +
"from $startMonth to $endMonth"
"from $startMonth to $endMonth"
}
class TreeHouse(
@ -46,8 +45,6 @@ fun main() {
vacationHouse eq
"Vacation house at 8 Target St., " +
"KS 66632 from May to September"
val tree = TreeHouse("Oak")
tree eq
TreeHouse("Oak") eq
"Oak tree house at Tree Street, TR 00000"
}

View File

@ -2,9 +2,9 @@ type: theory
files:
- name: src/GreatApe3.kt
visible: true
- name: src/NoArgConstructor.kt
visible: true
- name: src/House.kt
visible: true
- name: src/OtherConstructors.kt
visible: true
- name: src/NoArgConstructor.kt
visible: true

View File

@ -0,0 +1,7 @@
// ClassDelegation/BasicDelegation.kt
package classdelegation
interface AI
class A : AI
class B(val a: A) : AI by a

View File

@ -5,7 +5,7 @@ import atomictest.eq
class DelegatedControls(
private val controls: SpaceShipControls =
SpaceShipControls()
): ShipControls by controls {
): Controls by controls {
override fun turboBoost(): String =
"${controls.turboBoost()}... boooooost!"
}

View File

@ -2,9 +2,9 @@
package classdelegation
import atomictest.eq
class ExplicitControls : ShipControls {
class ExplicitControls : Controls {
private val controls = SpaceShipControls()
// Delegated members:
// Delegation by hand:
override fun up(velocity: Int) =
controls.up(velocity)
override fun back(velocity: Int) =

View File

@ -32,9 +32,9 @@ class UserInput : MouseManager {
class Button(
val width: Int,
val height: Int,
val image: Rectangle =
var image: Rectangle =
ButtonImage(width, height),
val input: MouseManager = UserInput()
private var input: MouseManager = UserInput()
): Rectangle by image, MouseManager by input
fun main() {

View File

@ -1,7 +1,7 @@
// ClassDelegation/SpaceShipControls.kt
package classdelegation
interface ShipControls {
interface Controls {
fun up(velocity: Int): String
fun down(velocity: Int): String
fun left(velocity: Int): String
@ -11,7 +11,7 @@ interface ShipControls {
fun turboBoost(): String
}
class SpaceShipControls : ShipControls {
class SpaceShipControls : Controls {
override fun up(velocity: Int) =
"up $velocity"
override fun down(velocity: Int) =

View File

@ -8,3 +8,5 @@ files:
visible: true
- name: src/ModelingMI.kt
visible: true
- name: src/BasicDelegation.kt
visible: true

View File

@ -24,12 +24,5 @@ fun main() {
utility1(mc)
utility2(mc)
useMyClass(mc)
trace eq """
h()
g()
g()
h()
g()
h()
"""
trace eq "h() g() g() h() g() h()"
}

View File

@ -24,12 +24,5 @@ fun main() {
utility1(mc)
utility2(mc)
useMyClass(mc.field)
trace eq """
h()
g()
g()
h()
g()
h()
"""
trace eq "h() g() g() h() g() h()"
}

View File

@ -11,16 +11,11 @@ open class Bonobo : GreatApe()
class Chimpanzee : GreatApe()
class BonoboB : Bonobo()
fun info(ape: GreatApe) =
"weight: ${ape.weight} age: ${ape.age}"
fun GreatApe.info() = "wt: $weight age: $age"
fun main() {
info(GreatApe()) eq
"weight: 100.0 age: 12"
info(Bonobo()) eq
"weight: 100.0 age: 12"
info(Chimpanzee()) eq
"weight: 100.0 age: 12"
info(BonoboB()) eq
"weight: 100.0 age: 12"
GreatApe().info() eq "wt: 100.0 age: 12"
Bonobo().info() eq "wt: 100.0 age: 12"
Chimpanzee().info() eq "wt: 100.0 age: 12"
BonoboB().info() eq "wt: 100.0 age: 12"
}

View File

@ -27,7 +27,7 @@ class Bonobo : GreatApe() {
}
class Chimpanzee : GreatApe() {
// New property
// New property:
val additionalEnergy = 20
override fun call() = "Yawp!"
override fun eat() {

View File

@ -5,19 +5,20 @@ import atomictest.eq
private fun messy(): String {
val built = StringBuilder() // [1]
built.append("ABCs: ")
('a'..'z').forEach { built.append(it) }
('a'..'x').forEach { built.append(it) }
return built.toString() // [2]
}
private fun clean() = buildString {
append("ABCs: ")
('a'..'z').forEach { append(it) }
('a'..'x').forEach { append(it) }
}
private fun cleaner() =
('a'..'z').joinToString("", "ABCs: ")
('a'..'x').joinToString("", "ABCs: ")
fun main() {
messy() eq "ABCs: abcdefghijklmnopqrstuvwx"
messy() eq clean()
clean() eq cleaner()
}

View File

@ -17,7 +17,7 @@ data class Molecule(
fun main() {
val m1 = Molecule()
val m2 = Molecule()
m1 + m2 // [1]
m1 + m2 // [1]
m1 eq "Molecule(id=0, attached=" +
"Molecule(id=1, attached=null))"
}

View File

@ -12,10 +12,7 @@ class DataFile(val fileName: String) :
if (!targetDir.exists())
targetDir.mkdir()
}
fun erase() {
if (exists())
delete()
}
fun erase() { if (exists()) delete() }
fun reset(): File {
erase()
createNewFile()

View File

@ -5,8 +5,8 @@ import atomictest.CapturedException
fun capture(f:() -> Unit): CapturedException =
try { // [1]
f()
CapturedException(null, // [2]
"<Error>: Expected an exception")
CapturedException(null,
"<Error>: Expected an exception") // [2]
} catch (e: Throwable) { // [3]
CapturedException(e::class, // [4]
if (e.message != null) ": ${e.message}"

View File

@ -7,12 +7,12 @@ class Exception1(
): Exception("wrong value: $value")
open class Exception2(
message: String
): Exception(message)
description: String
): Exception(description)
class Exception3(
message: String
): Exception2(message)
description: String
): Exception2(description)
fun main() {
capture {

View File

@ -5,7 +5,7 @@ import atomictest.*
fun testCode(code: Int) {
if (code <= 1000) {
throw IllegalArgumentException(
"Code must be > 1000: $code")
"'code' must be > 1000: $code")
}
}
@ -15,7 +15,7 @@ fun main() {
testCode("A1".toInt(16))
} catch (e: IllegalArgumentException) {
e.message eq
"Code must be > 1000: 161"
"'code' must be > 1000: 161"
}
try {
testCode("0".toInt(1))

View File

@ -11,7 +11,7 @@ class Logger(fileName: String) {
fun info(msg: String) = log("Info", msg)
fun warn(msg: String) = log("Warn", msg)
fun error(msg: String) = log("Error", msg)
// Added for basic testing:
// For basic testing:
fun report(msg: String) {
trace(msg)
debug(msg)

View File

@ -8,7 +8,7 @@ val logFile = // Reset ensures an empty file:
fun debug(msg: String) =
System.err.println("Debug: $msg")
// To disable:
// fun debug(msg: String) { /* do nothing */ }
// fun debug(msg: String) = Unit
fun trace(msg: String) =
logFile.appendText("Trace: $msg\n")

View File

@ -7,10 +7,11 @@ private val logger = Logger("AtomicLog.txt")
fun main() {
logger.report("Hello, Atomic Log!")
logger.logFile.readText() eq
"""Trace: Hello, Atomic Log!
Debug: Hello, Atomic Log!
Info: Hello, Atomic Log!
Warn: Hello, Atomic Log!
Error: Hello, Atomic Log!"""
logger.logFile.readText() eq """
Trace: Hello, Atomic Log!
Debug: Hello, Atomic Log!
Info: Hello, Atomic Log!
Warn: Hello, Atomic Log!
Error: Hello, Atomic Log!
"""
}

View File

@ -8,16 +8,16 @@ fun checkObject(obj: Any?): String =
if (obj is String)
obj
else
throw BadData("Need String, got $obj")
throw BadData("Needs String, got $obj")
fun test(checkObj: (obj: Any?) -> String) {
checkObj("abc") eq "abc"
capture {
checkObj(null)
} eq "BadData: Need String, got null"
} eq "BadData: Needs String, got null"
capture {
checkObj(123)
} eq "BadData: Need String, got 123"
} eq "BadData: Needs String, got 123"
}
fun main() {

View File

@ -2,7 +2,7 @@
package nothingtype
fun failWithBadData(obj: Any?): Nothing =
throw BadData("Need String, got $obj")
throw BadData("Needs String, got $obj")
fun checkObject2(obj: Any?): String =
(obj as? String) ?: failWithBadData(obj)

View File

@ -4,9 +4,9 @@ fun condition(i: Int) = i < 100 // [1]
fun main() {
var i = 0
while (condition(i)) {
while (condition(i)) { // [2]
print(".")
i += 10 // [2]
i += 10 // [3]
}
}
/* Output:

View File

@ -1,7 +1,7 @@
// Enumerations/RecursiveEnumImport.kt
package enumerations
import atomictest.eq
import enumerations.Size.* // [1]
import enumerations.Size.* // [1]
enum class Size {
Tiny, Small, Medium, Large, Huge, Gigantic

View File

@ -1113,17 +1113,17 @@ public class TestAllExamples extends AbstractTestExamples {
@Test
public void testNonNullAssertCall() {
testExample("Usability/Non-null Assertions/Examples/src/NonNullAssertCall.kt", NonNullAssertCallKt::main);
testExample("Usability/Non-Null Assertions/Examples/src/NonNullAssertCall.kt", NonNullAssertCallKt::main);
}
@Test
public void testNonNullAssert() {
testExample("Usability/Non-null Assertions/Examples/src/NonNullAssert.kt", NonNullAssertKt::main);
testExample("Usability/Non-Null Assertions/Examples/src/NonNullAssert.kt", NonNullAssertKt::main);
}
@Test
public void testValueFromMap() {
testExample("Usability/Non-null Assertions/Examples/src/ValueFromMap.kt", ValueFromMapKt::main);
testExample("Usability/Non-Null Assertions/Examples/src/ValueFromMap.kt", ValueFromMapKt::main);
}
@Test
@ -1721,6 +1721,11 @@ public class TestAllExamples extends AbstractTestExamples {
testExample("Object-Oriented Programming/Base Class Initialization/Examples/src/House.kt", baseclassinit.HouseKt::main);
}
@Test
public void testPropertyAccessor() {
testExample("Object-Oriented Programming/Abstract Classes/Examples/src/PropertyAccessor.kt", abstractclasses.PropertyAccessorKt::main);
}
@Test
public void testImplementations() {
testExample("Object-Oriented Programming/Abstract Classes/Examples/src/Implementations.kt", abstractclasses.ImplementationsKt::main);
@ -1736,11 +1741,6 @@ public class TestAllExamples extends AbstractTestExamples {
testExample("Object-Oriented Programming/Abstract Classes/Examples/src/StateOfAClass.kt", abstractstate.StateOfAClassKt::main);
}
@Test
public void testWithPropertyAccessor() {
testExample("Object-Oriented Programming/Abstract Classes/Examples/src/WithPropertyAccessor.kt", abstractclasses.WithPropertyAccessorKt::main);
}
@Test
public void testAssignment() {
testExample("Object-Oriented Programming/Upcasting/Examples/src/Assignment.kt", AssignmentKt::main);
@ -3408,17 +3408,17 @@ public class TestAllExamples extends AbstractTestExamples {
@Test
public void testTask146() {
testExample("Usability/Non-null Assertions/Exercise 1/src/Task.kt", nonNullAssertionsExercise1.TaskKt::main);
testExample("Usability/Non-Null Assertions/Exercise 1/src/Task.kt", nonNullAssertionsExercise1.TaskKt::main);
}
@Test
public void testTask147() {
testExample("Usability/Non-null Assertions/Exercise 2/src/Task.kt", nonNullAssertionsExercise2.TaskKt::main);
testExample("Usability/Non-Null Assertions/Exercise 2/src/Task.kt", nonNullAssertionsExercise2.TaskKt::main);
}
@Test
public void testTask148() {
testExample("Usability/Non-null Assertions/Exercise 3/src/Task.kt", nonNullAssertionsExercise3.TaskKt::main);
testExample("Usability/Non-Null Assertions/Exercise 3/src/Task.kt", nonNullAssertionsExercise3.TaskKt::main);
}
@Test