github.com/gogf/gf@v1.16.9/os/gctx/gctx_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gctx_test 8 9 import ( 10 "context" 11 "github.com/gogf/gf/os/gctx" 12 "github.com/gogf/gf/text/gstr" 13 "testing" 14 15 "github.com/gogf/gf/test/gtest" 16 ) 17 18 func Test_New(t *testing.T) { 19 gtest.C(t, func(t *gtest.T) { 20 ctx := gctx.New() 21 t.AssertNE(ctx, nil) 22 t.AssertNE(gctx.Value(ctx), "") 23 }) 24 } 25 26 func Test_WithCtx(t *testing.T) { 27 gtest.C(t, func(t *gtest.T) { 28 ctx := context.WithValue(context.TODO(), "TEST", 1) 29 ctx = gctx.WithCtx(ctx) 30 t.AssertNE(gctx.Value(ctx), "") 31 t.Assert(ctx.Value("TEST"), 1) 32 }) 33 } 34 35 func Test_WithPrefix(t *testing.T) { 36 gtest.C(t, func(t *gtest.T) { 37 ctx := context.WithValue(context.TODO(), "TEST", 1) 38 ctx = gctx.WithPrefix(ctx, "H-") 39 t.Assert(gstr.Contains(gctx.Value(ctx), "H-"), true) 40 t.Assert(ctx.Value("TEST"), 1) 41 }) 42 } 43 44 func Test_WithValue(t *testing.T) { 45 gtest.C(t, func(t *gtest.T) { 46 ctx := context.WithValue(context.TODO(), "TEST", 1) 47 ctx = gctx.WithValue(ctx, "123") 48 t.Assert(gctx.Value(ctx), "123") 49 t.Assert(ctx.Value("TEST"), 1) 50 }) 51 }