diff --git a/dot-net/Centroid.Tests/ConfigTest.cs b/dot-net/Centroid.Tests/ConfigTest.cs index aca6357..a663d2a 100644 --- a/dot-net/Centroid.Tests/ConfigTest.cs +++ b/dot-net/Centroid.Tests/ConfigTest.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; using System.Text.RegularExpressions; using Microsoft.CSharp.RuntimeBinder; @@ -168,9 +168,9 @@ public void test_enumerated_json_object_values_are_still_shiny() } }"; dynamic config = new Config(json); - foreach (var kvp in config.Connections) + foreach (var kvp in config.Connections) { - Assert.That(kvp.Value.Password, Is.EqualTo("secret")); + Assert.That(kvp.Value.Password, Is.EqualTo("secret")); } } @@ -215,5 +215,13 @@ public void test_contains_key() Assert.That(config.ContainsKey("theEnvironment"), Is.True); Assert.That(config.ContainsKey("DoesNotExist"), Is.False); } + + [Test] + public void test_key_as_index() + { + dynamic config = new Config(JsonConfig); + var myString = "thekey"; + Assert.That(config.theEnvironment[myString], Is.EqualTo("TheValue")); + } } } diff --git a/dot-net/Centroid.sln.DotSettings b/dot-net/Centroid.sln.DotSettings index 94f3b34..632a1e5 100644 --- a/dot-net/Centroid.sln.DotSettings +++ b/dot-net/Centroid.sln.DotSettings @@ -26,4 +26,7 @@ True True False - <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> + True + True + True diff --git a/dot-net/Centroid/Config.cs b/dot-net/Centroid/Config.cs index 0194769..0aa3d50 100644 --- a/dot-net/Centroid/Config.cs +++ b/dot-net/Centroid/Config.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections; using System.Collections.Generic; using System.Dynamic; @@ -35,6 +35,12 @@ public object this[int index] set { RawConfig[index] = value; } } + public object this[string index] + { + get { return GetValue(index); } + set { RawConfig[index] = value; } + } + public bool ContainsKey(string key) { return GetActualKey(key) != null; diff --git a/python/tests.py b/python/tests.py index 3794fac..f272111 100644 --- a/python/tests.py +++ b/python/tests.py @@ -1,5 +1,6 @@ import unittest import json +import os.path from centroid import Config class ConfigTest(unittest.TestCase): @@ -14,7 +15,7 @@ def _json_config_with_array(self): @property def _shared_file_path(self): - return 'config.json' + return os.path.normpath('../config.json') def test_create_from_string(self): config = Config(self._json_config) @@ -132,3 +133,10 @@ def test_has_key(self): config = Config(self._json_config) self.assertTrue("the_environment" in config) self.assertTrue("does_not_exist" not in config) + + def test_key_as_index(self): + config = Config(self._json_config) + my_string = "thekey" + self.assertEqual(config.the_environment[my_string], "TheValue") + +unittest.main() diff --git a/ruby/test/centroid_test.rb b/ruby/test/centroid_test.rb index 47822a0..389bfa7 100644 --- a/ruby/test/centroid_test.rb +++ b/ruby/test/centroid_test.rb @@ -12,7 +12,7 @@ def json_config_with_array end def shared_file_path - 'config.json' + File.join('..','..','config.json') end def test_create_from_string @@ -95,35 +95,6 @@ def test_environment_specific_config_overrides_all assert_equal(config.shared, "production!") end - def test_all_environment_is_not_case_sensitive - config = Centroid::Config.new('{"Prod": {"Shared": "production!"}, "All": {"Shared": "none", "AllOnly": "works"}}') - config = config.for_environment("Prod") - assert_equal(config.all_only, "works") - - config = Centroid::Config.new('{"Prod": {"Shared": "production!"}, "all": {"Shared": "none", "AllOnly": "works"}}') - config = config.for_environment("Prod") - assert_equal(config.all_only, "works") - end - - def test_supports_deep_merge - config = Centroid::Config.new('{"Prod": {"Database": {"Server": "prod-sql"}}, "All": {"Database": {"MigrationsPath": "path/to/migrations"}}}') - config = config.for_environment("Prod") - assert_equal(config.database.server, "prod-sql") - assert_equal(config.database.migrations_path, "path/to/migrations") - end - - def test_has_key - config = Centroid::Config.new(json_config) - assert(config.has_key?("the_environment")) - assert(!config.has_key?("does_not_exist")) - end - - def test_respond_to - config = Centroid::Config.new(json_config) - assert(config.respond_to?(:the_environment)) - assert(!config.respond_to?(:does_not_exist)) - end - def test_indexing_json_array config = Centroid::Config.new(json_config_with_array) assert_equal(config.the_array[0].the_key, "Value1") @@ -169,4 +140,40 @@ def test_enumerated_json_object_values_are_still_shiny assert_equal(v.password, "secret") end end + + def test_all_environment_is_not_case_sensitive + config = Centroid::Config.new('{"Prod": {"Shared": "production!"}, "All": {"Shared": "none", "AllOnly": "works"}}') + config = config.for_environment("Prod") + assert_equal(config.all_only, "works") + + config = Centroid::Config.new('{"Prod": {"Shared": "production!"}, "all": {"Shared": "none", "AllOnly": "works"}}') + config = config.for_environment("Prod") + assert_equal(config.all_only, "works") + end + + def test_supports_deep_merge + config = Centroid::Config.new('{"Prod": {"Database": {"Server": "prod-sql"}}, "All": {"Database": {"MigrationsPath": "path/to/migrations"}}}') + config = config.for_environment("Prod") + assert_equal(config.database.server, "prod-sql") + assert_equal(config.database.migrations_path, "path/to/migrations") + end + + def test_has_key + config = Centroid::Config.new(json_config) + assert(config.has_key?("the_environment")) + assert(!config.has_key?("does_not_exist")) + end + + def test_key_as_index + config = Centroid::Config.new(json_config) + my_string = "thekey" + assert_equal(config.the_environment[my_string], "TheValue") + end + + def test_respond_to + config = Centroid::Config.new(json_config) + assert(config.respond_to?(:the_environment)) + assert(!config.respond_to?(:does_not_exist)) + end + end