1
1
Fork 0

Fighting with untestable exercises in 'Data Types'

This commit is contained in:
Svetlana Isakova 2019-10-18 20:13:24 +02:00
parent 1ec72cc977
commit 8671a04375
25 changed files with 381 additions and 233 deletions

View File

@ -1,7 +1,7 @@
package creatingClasses4 package creatingClasses4
fun main() { fun main() {
val s = "Hello!" val s: String = "Hello!"
println(s.toUpperCase()) println(s.toUpperCase())
println(s.toLowerCase()) println(s.toLowerCase())
} }

View File

@ -1,13 +1,15 @@
## Mastering the IDE: Quick Documentation ## Mastering the IDE: Quick Documentation
The <span class="control">`Quick Documentation`</span> action produces We've already used <span class="control">`Quick Documentation`</span> action
information about a symbol, such as a function or a class. to check the variable type.
Press <span class="shortcut">&shortcut:QuickJavaDoc;</span> when the caret The <span class="control">`Quick Documentation`</span> action produces
is on a class or a function, and IntelliJ Idea will show you the available information about any symbol, it can be used to check information about a
documentation for this function or class. function or a class. Press <span class="shortcut">&shortcut:QuickJavaDoc;</span>
when the caret is on a class or a function usage, and IntelliJ Idea will show
you the available documentation for this function or class.
Call <span class="control">`Quick Documentation`</span> action Call <span class="control">`Quick Documentation`</span> action putting the caret
for the `String` class and its `toUpperCase()` and `toLowerCase()` functions. on the `String` type and its `get()` and `length()` member functions.
Press <span class="shortcut">&shortcut:EditorEscape;</span> to close the Press <span class="shortcut">&shortcut:EditorEscape;</span> to close the
quick documentation pop-up. quick documentation pop-up.

View File

@ -1,4 +1,6 @@
## Mastering the IDE: Quick Documentation on a Variable ## Mastering the IDE: TODO
Quick Documentation on a Variable
You can press <span class="shortcut">&shortcut:QuickJavaDoc;</span> You can press <span class="shortcut">&shortcut:QuickJavaDoc;</span>
or run a <span class="control">`Quick Documentation`</span> action or run a <span class="control">`Quick Documentation`</span> action

View File

@ -1 +1,34 @@
package dataTypesExercise2 package dataTypesExercise2
fun main() {
val int: Int = 10
val double: Double = 1.1
val boolean: Boolean = false
val string: String = "abc"
val character: Char = 'a'
// Can be combined:
// String can be combined with every other type:
val si: String = string + int
val sd: String = string + double
val sb: String = string + boolean
val sc: String = string + character
val d1: Double = int + double
val d2: Double = double + int
val s1: String = character + string
val c1: Char = character + int
println("The type that can be combined " +
"with every other type using '+':")
println("String")
// Can't be combined:
// val bb = boolean + boolean
// val ic = int + character
// val ds = double + string
println("The type that can't be combined " +
"with itself using '+':")
println("Boolean")
}

View File

@ -2,5 +2,18 @@ type: edu
files: files:
- name: src/Task.kt - name: src/Task.kt
visible: true visible: true
- name: test/Tests.kt placeholders:
- offset: 201
length: 325
placeholder_text: TODO() // write some examples
- offset: 624
length: 6
placeholder_text: ???
- offset: 660
length: 89
placeholder_text: TODO() // write some examples
- offset: 839
length: 7
placeholder_text: ???
- name: test/output.txt
visible: false visible: false

View File

@ -1,9 +1,7 @@
## Data Types (#2) ## Data Types (#2)
{{ :UNTESTABLE It's impossible to analyze the local variables by reflection }} Attempt to combine the various `val`s of different types using the `+`
operator. Only combine two at a time, and assign each combination to a `val`
Remove all the type declarations from `Types.kt` and verify that Kotlin result. See which types combine. Display to the console the name of the type
successfully infers the types. Now define duplicate but typed definitions which that can be combined with any other type if it goes first, and the name of the
are initialized by the inferred `val`s. Use a trailing underscore for the type that can't be combined with itself using `+`.
duplicate definition, for example `val whole_: Int = whole`. Ensure that all
the typed definitions match the type inference by Kotlin.

View File

@ -1,9 +0,0 @@
package dataTypesExercise2
import org.junit.Test
import util.untestable
class TestDataTypesExercise2 {
@Test
fun test() = untestable()
}

View File

@ -0,0 +1,4 @@
The type that can be combined with every other type using '+':
String
The type that can't be combined with itself using '+':
Boolean

View File

@ -1 +1,10 @@
package dataTypesExercise3 package dataTypesExercise3
// 'a' + 1
val c1 = 'b'
// 'a' + 25
val c2 = 'z'
// 'E' - 2
val c3 = 'C'

View File

@ -2,5 +2,15 @@ type: edu
files: files:
- name: src/Task.kt - name: src/Task.kt
visible: true visible: true
placeholders:
- offset: 48
length: 3
placeholder_text: '''?'''
- offset: 74
length: 3
placeholder_text: '''?'''
- offset: 99
length: 3
placeholder_text: '''?'''
- name: test/Tests.kt - name: test/Tests.kt
visible: false visible: false

View File

@ -1,9 +1,20 @@
## Data Types (#3) ## Data Types (#3)
{{ :UNTESTABLE No specific output is expected }} Guess the results of the following expressions and then check yourself using
Kotlin:
Attempt to combine the various `val`s defined in `Types.kt` using the `+` ```kotlin
operator. Only combine two at a time, and assign each combination to a `val` val c1 = 'a' + 1
result. Perform an exhaustive set of combinations (every identifier with every val c2 = 'a' + 25
other identifier), and see which types combine (if any do). Now add type val c3 = 'E' - 2
annotations to the new `val`s which successfully combine. ```
Open the 'hint' below to see the explanation of why it works like that.
<div class="hint">
Characters are stored as numbers corresponding to their
[ASCII codes](https://en.wikipedia.org/wiki/ASCII), so adding an integer to a
character produces a new character corresponding to the new code value.
</div>

View File

@ -1,9 +1,26 @@
package dataTypesExercise3 package dataTypesExercise3
import org.junit.Assert
import org.junit.FixMethodOrder
import org.junit.Test import org.junit.Test
import org.junit.runners.MethodSorters
import util.TIMEOUT
import util.untestable import util.untestable
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
class TestDataTypesExercise3 { class TestDataTypesExercise3 {
@Test @Test(timeout = TIMEOUT)
fun test() = untestable() fun testA() {
Assert.assertEquals("Wrong result for 'c1'", 'b', c1)
}
@Test(timeout = TIMEOUT)
fun testB() {
Assert.assertEquals("Wrong result for 'c2'", 'z', c2)
}
@Test(timeout = TIMEOUT)
fun testC() {
Assert.assertEquals("Wrong result for 'c3'", 'C', c3)
}
} }

View File

@ -1,6 +1,28 @@
package dataTypes4 package dataTypesExercise4
fun main() { fun main() {
val s = "Sally" + 5.9 val whole = 11
println(s) val fractional = 1.4
} val trueOrFalse = true
val words = "A value"
val character = 'z'
val lines = """Triple quotes let
you have many lines
in your string"""
println(whole)
println(fractional)
println(trueOrFalse)
println(words)
println(character)
println(lines)
}
/* Output:
11
1.4
true
A value
z
Triple quotes let
you have many lines
in your string
*/

View File

@ -1,8 +1,6 @@
type: ide type: edu
files: files:
- name: src/Task.kt - name: src/Task.kt
visible: true visible: true
- name: test/Tests.kt - name: test/Tests.kt
visible: false visible: false
feedback_link: |
https://docs.google.com/forms/d/e/1FAIpQLSdkaliSwYkjiV21bZl0yP-In2g5p17sAQCfaGjyHx_QYMWTiQ/viewform?usp=pp_url&entry.189755027=Programming+Basics+%2F+Data+Types+%2F+Exercise2

View File

@ -1,10 +1,12 @@
## Mastering the IDE: "Specify Type" Intention ## Mastering the IDE: Checking the inferred variable type
IntelliJ IDEA helps you automatically add type specifications for `val`s and In the accompanying example, all the type declarations from `Types.kt` are
`var`s. Put the caret right after `s` and press removed. Verify that Kotlin successfully infers the types.
<span class="shortcut">&shortcut:ShowIntentionActions;</span>. This is
the shortcut for <span class="control">`Show intention actions`</span>.
Choose <span class="control">`Specify type explicitly`</span> in the dropdown menu.
Different intention actions are available in different contexts, depending on You can press <span class="shortcut">&shortcut:QuickJavaDoc;</span>
where you put the caret. while having the caret on an identifier to check the type Kotlin infers
for this identifier.
Press <span class="shortcut">&shortcut:EditorEscape;</span> to close the pop-up.
Check the types of all the declared variables in the example.

View File

@ -1 +1 @@
package dataTypes4 package dataTypesExercise4

View File

@ -0,0 +1,6 @@
package dataTypesExercise5
fun main() {
val s = "Sally" + 5.9
println(s)
}

View File

@ -0,0 +1,8 @@
type: ide
files:
- name: src/Task.kt
visible: true
- name: test/Tests.kt
visible: false
feedback_link: |
https://docs.google.com/forms/d/e/1FAIpQLSdkaliSwYkjiV21bZl0yP-In2g5p17sAQCfaGjyHx_QYMWTiQ/viewform?usp=pp_url&entry.189755027=Programming+Basics+%2F+Data+Types+%2F+Exercise2

View File

@ -0,0 +1,10 @@
## Mastering the IDE: "Specify Type" Intention
IntelliJ IDEA helps you automatically add type specifications for `val`s and
`var`s. Put the caret right after `s` and press
<span class="shortcut">&shortcut:ShowIntentionActions;</span>. This is
the shortcut for <span class="control">`Show intention actions`</span>.
Choose <span class="control">`Specify type explicitly`</span> in the dropdown menu.
Different intention actions are available in different contexts, depending on
where you put the caret.

View File

@ -0,0 +1 @@
package dataTypesExercise5

View File

@ -4,3 +4,4 @@ content:
- Exercise 2 - Exercise 2
- Exercise 3 - Exercise 3
- Exercise 4 - Exercise 4
- Exercise 5

View File

@ -10,21 +10,21 @@ import util.TIMEOUT
class TestNumberTypesExercise1 { class TestNumberTypesExercise1 {
@Test(timeout = TIMEOUT) @Test(timeout = TIMEOUT)
fun testA() { fun testA() {
Assert.assertEquals(3, a) Assert.assertEquals("Wrong result for 'a'", 3, a)
} }
@Test(timeout = TIMEOUT) @Test(timeout = TIMEOUT)
fun testB() { fun testB() {
Assert.assertEquals(2, b) Assert.assertEquals("Wrong result for 'b'", 2, b)
} }
@Test(timeout = TIMEOUT) @Test(timeout = TIMEOUT)
fun testC() { fun testC() {
Assert.assertEquals(11, c) Assert.assertEquals("Wrong result for 'c'", 11, c)
} }
@Test(timeout = TIMEOUT) @Test(timeout = TIMEOUT)
fun testD() { fun testD() {
Assert.assertEquals(1.2, d.toDouble(), 0.0001) Assert.assertEquals("Wrong result for 'd'", 1.2, d.toDouble(), 0.0001)
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -24,7 +24,7 @@ class TestAllExercises : AbstractTestExercises() {
fun testDataTypesExercise1() = testClass(dataTypesExercise1.TestDataTypesExercise1::class) fun testDataTypesExercise1() = testClass(dataTypesExercise1.TestDataTypesExercise1::class)
@Test @Test
fun testDataTypesExercise2() = testClass(dataTypesExercise2.TestDataTypesExercise2::class) fun testDataTypesExercise2() = testOutput("../AtomicKotlinCourse/Programming Basics/Data Types/Exercise 2/test/output.txt"){ dataTypesExercise2.main() }
@Test @Test
fun testDataTypesExercise3() = testClass(dataTypesExercise3.TestDataTypesExercise3::class) fun testDataTypesExercise3() = testClass(dataTypesExercise3.TestDataTypesExercise3::class)