# frozen_string_literal: true require_relative "helper" class SettingsTest < Minitest::Test def setup @app = new_app end def test_set_and_get @app.set(:foo, "foo") assert_equal "foo", @app.settings[:foo] end def test_set_hash @app.set(:foo, bar: "bar") assert_equal "bar", @app.settings[:foo][:bar] end def test_set_hash_with_merge @app.set(:foo, foo: "foo", bar: "bar") @app.set(:foo, bar: "foo", baz: "baz") assert_equal "foo", @app.settings[:foo][:foo] assert_equal "foo", @app.settings[:foo][:bar] assert_equal "baz", @app.settings[:foo][:baz] end def test_set_hash_without_merge @app.set(:foo, bar: "bar") @app.set!(:foo, baz: "baz") assert_equal nil, @app.settings[:foo][:bar] assert_equal "baz", @app.settings[:foo][:baz] end def test_inheritance @app.set(:foo, "foo") @child1 = new_app(parent: @app) @child2 = new_app(parent: @child1) assert_equal "foo", @child1.settings[:foo] assert_equal "foo", @child2.settings[:foo] @child2.set(:foo, "bar") assert_equal "foo", @child1.settings[:foo] assert_equal "bar", @child2.settings[:foo] end def test_inheritance_deep_dup @app.set(:foo, bar: ["bar"], baz: { foo: "baz" }) @child1 = new_app(parent: @app) @child1.set(:foo, foo: "foo") @child1.settings[:foo][:bar].push("foo") @child1.settings[:foo][:baz][:foo] = "foo" assert_equal ["bar"], @app.settings[:foo][:bar] assert_equal "baz", @app.settings[:foo][:baz][:foo] end def test_freeze @app.set(:foo, foo: "foo", bar: { foo: "foo" }) @app.define do on get do self.class.settings[:foo][:foo] = "bar" end on post do self.class.settings[:foo][:bar][:foo] = "bar" end end ts = Tynn::Test.new(@app) assert_raises { ts.get("/") } assert_raises { ts.post("/") } end end