v / vlib / context / value_test.v
35 lines · 30 sloc · 652 bytes · a80bc2331450fc28c900097f8afafe173f161d27
Raw
1// vtest build: amd64 || arm64
2import context
3
4const not_found_value = &Value{
5 val: 'key not found'
6}
7
8struct Value {
9 val string
10}
11
12// This example demonstrates how a value can be passed to the context
13// and also how to retrieve it if it exists.
14fn test_with_value() {
15 f := fn (ctx context.Context, key context.Key) &Value {
16 if value := ctx.value(key) {
17 match value {
18 Value {
19 return value
20 }
21 else {}
22 }
23 }
24 return not_found_value
25 }
26
27 key := 'language'
28 value := &Value{
29 val: 'VAL'
30 }
31 ctx := context.with_value(context.background(), key, value)
32
33 assert value == f(ctx, key)
34 assert not_found_value == f(ctx, 'color')
35}
36