@@ -22,7 +22,7 @@ import (
2222 "github.com/stretchr/testify/require"
2323)
2424
25- func TestProviderMethods (t * testing.T ) {
25+ func newProvider (t testing.TB ) * Provider {
2626 // Fake some flags
2727 f := pflag .NewFlagSet ("config" , pflag .ContinueOnError )
2828 f .String ("foo-bar-baz" , "" , "" )
@@ -32,10 +32,15 @@ func TestProviderMethods(t *testing.T) {
3232 RegisterFlags (f )
3333
3434 ctx , cancel := context .WithCancel (context .Background ())
35- defer cancel ( )
35+ t . Cleanup ( cancel )
3636
3737 p , err := New (ctx , []byte (`{"type": "object", "properties": {"foo-bar-baz": {"type": "string"}, "b": {"type": "string"}}}` ), WithFlags (f ), WithContext (ctx ))
3838 require .NoError (t , err )
39+ return p
40+ }
41+
42+ func TestProviderMethods (t * testing.T ) {
43+ p := newProvider (t )
3944
4045 t .Run ("check flags" , func (t * testing.T ) {
4146 assert .Equal (t , "fff" , p .String ("foo-bar-baz" ))
@@ -106,6 +111,21 @@ func TestProviderMethods(t *testing.T) {
106111 assert .NoError (t , p .Set ("nested.value" , "https://www.ory.sh/kratos" ))
107112 assert .Equal (t , "https://www.ory.sh/kratos" , p .Get ("nested.value" ))
108113 })
114+
115+ t .Run ("use DirtyPatch operations" , func (t * testing.T ) {
116+ assert .NoError (t , p .DirtyPatch ("nested" , nil ))
117+ assert .NoError (t , p .DirtyPatch ("nested.value" , "https://www.ory.sh/kratos" ))
118+ assert .Equal (t , "https://www.ory.sh/kratos" , p .Get ("nested.value" ))
119+
120+ assert .NoError (t , p .DirtyPatch ("duration.integer1" , - 1 ))
121+ assert .NoError (t , p .DirtyPatch ("duration.integer2" , "-1" ))
122+ assert .Equal (t , - 1 * time .Nanosecond , p .DurationF ("duration.integer1" , time .Second ))
123+ assert .Equal (t , - 1 * time .Nanosecond , p .DurationF ("duration.integer2" , time .Second ))
124+
125+ require .NoError (t , p .DirtyPatch ("some.float" , 123.123 ))
126+ assert .Equal (t , 123.123 , p .Float64F ("some.float" , 321.321 ))
127+ assert .Equal (t , 321.321 , p .Float64F ("not.some.float" , 321.321 ))
128+ })
109129}
110130
111131func TestAdvancedConfigs (t * testing.T ) {
@@ -212,3 +232,27 @@ func TestAdvancedConfigs(t *testing.T) {
212232 })
213233 }
214234}
235+
236+ func BenchmarkSet (b * testing.B ) {
237+ // Benchmark set function
238+ p := newProvider (b )
239+ var err error
240+ for i := 0 ; i < b .N ; i ++ {
241+ err = p .Set ("nested.value" , "https://www.ory.sh/kratos" )
242+ if err != nil {
243+ b .Fatalf ("Unexpected error: %s" , err )
244+ }
245+ }
246+ }
247+
248+ func BenchmarkDirtyPatch (b * testing.B ) {
249+ // Benchmark set function
250+ p := newProvider (b )
251+ var err error
252+ for i := 0 ; i < b .N ; i ++ {
253+ err = p .DirtyPatch ("nested.value" , "https://www.ory.sh/kratos" )
254+ if err != nil {
255+ b .Fatalf ("Unexpected error: %s" , err )
256+ }
257+ }
258+ }
0 commit comments