|
| 1 | + |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.util.Scanner; |
| 5 | + |
| 6 | +import org.junit.Assert; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +class UniverseTest { |
| 10 | + |
| 11 | + @Test fun test() { |
| 12 | + try { |
| 13 | + Assert.assertEquals("Running countAllStars(2, 3)...", 5, countAllStars(2, 3)) |
| 14 | + Assert.assertEquals("Running countAllStars(9, -3)...", 6, countAllStars(9, -3)) |
| 15 | + success(true) |
| 16 | + |
| 17 | + if (existsInFile("galaxies.sum()", File("./src/universe.kt"))) { |
| 18 | + msg("My personal Yoda, you are. 🙏", "* ● ¸ . ¸. :° ☾ ° ¸. ● ¸ . ¸. :. • ") |
| 19 | + msg("My personal Yoda, you are. 🙏", " ★ ° ☆ ¸. ¸ ★ :. . ") |
| 20 | + msg("My personal Yoda, you are. 🙏", "__.-._ ° . . . ☾ ° . * ¸ .") |
| 21 | + msg("My personal Yoda, you are. 🙏", "'-._\\7' . ° ☾ ° ¸.☆ ● . ") |
| 22 | + msg("My personal Yoda, you are. 🙏", " /'.-c * ● ¸. ° ° ¸. ") |
| 23 | + msg("My personal Yoda, you are. 🙏", " | /T ° ° ¸. ¸ . ") |
| 24 | + msg("My personal Yoda, you are. 🙏", "_)_/LI"); |
| 25 | + } else { |
| 26 | + msg("Kudos 🌟", "Did you know that in Kotlin you can use the sum() function directly on an Array? Try it!") |
| 27 | + msg("Kudos 🌟", "") |
| 28 | + msg("Kudos 🌟", "var galaxies = arrayOf(37, 3, 2)") |
| 29 | + msg("Kudos 🌟", "var totalStars = galaxies.sum() // 42") |
| 30 | + } |
| 31 | + } catch (ae: AssertionError) { |
| 32 | + success(false) |
| 33 | + msg("Oops! 🐞", ae.message) |
| 34 | + msg("Hint 💡", "Did you properly accumulate all stars into 'totalStars'? 🤔") |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + fun msg(channel: String, msg: String?) { |
| 39 | + System.out.println(String.format("TECHIO> message --channel \"%s\" \"%s\"", channel, msg)) |
| 40 | + } |
| 41 | + |
| 42 | + fun success(success: Boolean) { |
| 43 | + System.out.println(String.format("TECHIO> success %s", success)) |
| 44 | + } |
| 45 | + |
| 46 | + // check if a string exists in a text file |
| 47 | + fun existsInFile(str: String, file: File): Boolean { |
| 48 | + val scanner = Scanner(file) |
| 49 | + try { |
| 50 | + while (scanner.hasNextLine()) { |
| 51 | + if (scanner.nextLine().contains(str)) |
| 52 | + return true |
| 53 | + } |
| 54 | + return false; |
| 55 | + } finally { |
| 56 | + scanner.close() |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments