• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

leeonky / test-charm-java / 156

20 Mar 2025 01:53PM UTC coverage: 74.243% (-0.2%) from 74.475%
156

push

circleci

leeonky
Refactor

14 of 15 new or added lines in 12 files covered. (93.33%)

126 existing lines in 29 files now uncovered.

7947 of 10704 relevant lines covered (74.24%)

0.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.84
/DAL-java/src/main/java/com/github/leeonky/dal/runtime/schema/Actual.java
1
package com.github.leeonky.dal.runtime.schema;
2

3
import com.github.leeonky.dal.compiler.Compiler;
4
import com.github.leeonky.dal.format.Formatter;
5
import com.github.leeonky.dal.format.Type;
6
import com.github.leeonky.dal.format.Value;
7
import com.github.leeonky.dal.runtime.DalRuntimeException;
8
import com.github.leeonky.dal.runtime.Data;
9
import com.github.leeonky.dal.runtime.RuntimeContextBuilder.DALRuntimeContext;
10
import com.github.leeonky.dal.runtime.SchemaAssertionFailure;
11
import com.github.leeonky.dal.type.Schema;
12
import com.github.leeonky.dal.type.SubType;
13
import com.github.leeonky.util.BeanClass;
14

15
import java.util.Objects;
16
import java.util.function.Function;
17
import java.util.stream.Stream;
18

19
import static com.github.leeonky.util.Classes.getClassName;
20
import static java.lang.String.format;
21
import static java.util.Optional.ofNullable;
22

23
public class Actual {
24
    private final String property;
25
    private final Data actual;
26

27
    public Actual(String property, Data actual) {
1✔
28
        this.property = property;
1✔
29
        this.actual = actual;
1✔
30
    }
1✔
31

32
    public static Actual actual(Data data) {
33
        return new Actual("", data);
1✔
34
    }
35

36
    public Actual sub(Object property) {
37
        return new Actual(this.property + "." + property, actual.getValue(property));
1✔
38
    }
39

40
    public boolean isNull() {
41
        return actual.resolved().isNull();
1✔
42
    }
43

44
    public Actual sub(Integer index) {
UNCOV
45
        return new Actual(property + "[" + index + "]", actual.getValue(index));
×
46
    }
47

48
    private final static Compiler compiler = new Compiler();
1✔
49

50
    @SuppressWarnings("unchecked")
51
    public Class<Object> polymorphicSchemaType(Class<?> schemaType) {
52
        return ofNullable(schemaType.getAnnotation(SubType.class)).map(subType -> {
1✔
53
            Object subTypeProperty = actual.getValue(compiler.toChainNodes(subType.property())).instance();
1✔
54
            return (Class<Object>) Stream.of(subType.types()).filter(t -> t.value().equals(subTypeProperty))
1✔
55
                    .map(SubType.Type::type).findFirst().orElseThrow(() -> new DalRuntimeException(
1✔
56
                            format("Cannot guess sub type through property type value[%s]", subTypeProperty)));
1✔
57
        }).orElse((Class<Object>) schemaType);
1✔
58
    }
59

60
    public IllegalStateException invalidGenericType() {
61
        return new IllegalStateException(format("%s should specify generic type", property));
1✔
62
    }
63

64
    public boolean convertAble(BeanClass<?> type, String inspect) {
65
        if (isNull())
1✔
UNCOV
66
            return Verification.errorLog("Can not convert null field `%s` to %s, " +
×
67
                    "use @AllowNull to verify nullable field", property, inspect);
68
        try {
69
            actual.convert(type.getType()).resolve();
1✔
70
            return true;
1✔
71
        } catch (Exception ignore) {
1✔
72
            return Verification.errorLog("Can not convert field `%s` (%s: %s) to %s", property,
1✔
73
                    getClassName(actual.instance()), actual.instance(), inspect);
1✔
74
        }
75
    }
76

77
    public boolean verifyValue(Value<Object> value, BeanClass<?> type) {
78
        return value.verify(value.convertAs(actual, type))
1✔
79
                || Verification.errorLog(value.errorMessage(property, actual.instance()));
1✔
80
    }
81

82
    public Stream<?> fieldNames() {
83
        return actual.resolved().fieldNames().stream();
1✔
84
    }
85

86
    public Stream<Actual> subElements() {
87
        return actual.resolved().list().wraps().stream().map(data -> new Actual(property + "[" + data.index() + "]", data.value()));
1✔
88
    }
89

90
    public boolean verifyFormatter(Formatter<Object, Object> formatter) {
91
        return formatter.isValid(actual.instance())
1✔
92
                || Verification.errorLog("Expected field `%s` to be formatter `%s`\nActual: %s", property,
1✔
93
                formatter.getFormatterName(), actual.dump());
1✔
94
    }
95

96
    boolean verifySize(Function<Actual, Stream<?>> actualStream, int expectSize) {
97
        return actualStream.apply(this).count() == expectSize
1✔
98
                || Verification.errorLog("Expected field `%s` to be size <%d>, but was size <%d>",
1✔
99
                property, expectSize, actualStream.apply(this).count());
1✔
100
    }
101

102
    boolean moreExpectSize(int size) {
103
        return Verification.errorLog("Collection Field `%s` size was only <%d>, expected too more",
1✔
104
                property, size);
1✔
105
    }
106

107
    public boolean lessExpectSize(int size) {
UNCOV
108
        return Verification.errorLog("Expected collection field `%s` to be size <%d>, but too many elements", property, size);
×
109
    }
110

111
    boolean verifyType(Type<Object> expect) {
112
        return expect.verify(actual.instance()) ||
1✔
113
                Verification.errorLog(expect.errorMessage(property, actual.instance()));
1✔
114
    }
115

116
    boolean inInstanceOf(BeanClass<?> type) {
117
        return type.isInstance(actual.instance()) ||
1✔
118
                Verification.errorLog(String.format("Expected field `%s` to be %s\nActual: %s", property,
1✔
119
                        type.getName(), actual.dump()));
1✔
120
    }
121

122
    public boolean equalsExpect(Object expect, DALRuntimeContext runtimeContext) {
123
        return Objects.equals(expect, actual.instance()) ||
1✔
124
                Verification.errorLog(format("Expected field `%s` to be %s\nActual: %s", property,
1✔
125
                        runtimeContext.wrap(expect).dump(), actual.dump()));
1✔
126
    }
127

128
    public void verifySchema(Schema expect) {
129
        try {
130
            expect.verify(actual);
1✔
131
        } catch (SchemaAssertionFailure schemaAssertionFailure) {
1✔
UNCOV
132
            Verification.errorLog(schemaAssertionFailure.getMessage());
×
133
        }
1✔
134
    }
1✔
135
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc