|
| 1 | +package org.iiab.controller.util; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertFalse; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | + |
| 6 | +import org.json.JSONObject; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +/** Pure-JVM tests for the local_vars.yml flag reader. */ |
| 10 | +public class LocalVarsYamlParserTest { |
| 11 | + |
| 12 | + @Test |
| 13 | + public void readsInstallAndEnabledTrueVariants() { |
| 14 | + JSONObject j = LocalVarsYamlParser.parseToJson( |
| 15 | + "kiwix_install: True\n" + |
| 16 | + "kiwix_enabled: yes\n" + |
| 17 | + "kolibri_install: 1\n"); |
| 18 | + assertTrue(j.optBoolean("kiwix_install")); |
| 19 | + assertTrue(j.optBoolean("kiwix_enabled")); |
| 20 | + assertTrue(j.optBoolean("kolibri_install")); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + public void readsFalseValues() { |
| 25 | + JSONObject j = LocalVarsYamlParser.parseToJson("kiwix_install: False\nkiwix_enabled: no\n"); |
| 26 | + assertFalse(j.optBoolean("kiwix_install", true)); |
| 27 | + assertFalse(j.optBoolean("kiwix_enabled", true)); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void ignoresCommentsBlanksAndUnrelatedKeys() { |
| 32 | + JSONObject j = LocalVarsYamlParser.parseToJson( |
| 33 | + "# kiwix_install: True\n" + |
| 34 | + "\n" + |
| 35 | + "some_other_key: True\n" + |
| 36 | + "version: 3.2\n"); |
| 37 | + assertFalse(j.has("kiwix_install")); // commented out |
| 38 | + assertFalse(j.has("some_other_key")); // not an _install/_enabled flag |
| 39 | + assertFalse(j.has("version")); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void handlesValueContainingColon() { |
| 44 | + // split(":", 2) keeps everything after the first colon as the value. |
| 45 | + JSONObject j = LocalVarsYamlParser.parseToJson("note_enabled: true # at 10:30\n"); |
| 46 | + // value is "true # at 10:30" -> not exactly "true", so it reads false. |
| 47 | + assertFalse(j.optBoolean("note_enabled", true)); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void handlesNullAndEmpty() { |
| 52 | + assertFalse(LocalVarsYamlParser.parseToJson(null).keys().hasNext()); |
| 53 | + assertFalse(LocalVarsYamlParser.parseToJson("").keys().hasNext()); |
| 54 | + } |
| 55 | +} |
0 commit comments