Skip to content

Commit 25dd336

Browse files
authored
Merge pull request #3810 from asmirnov-backend/fix/#3793/EOnumberEOtimesTest
fix(#3793): EOnumber$EOtimesTest fix
2 parents 0580d49 + 7d67102 commit 25dd336

4 files changed

Lines changed: 64 additions & 11 deletions

File tree

eo-runtime/src/main/java/EOorg/EOeolang/EOnumber$EOtimes.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,13 @@ public final class EOnumber$EOtimes extends PhDefault implements Atom {
5757

5858
@Override
5959
public Phi lambda() {
60-
final Double left = new Dataized(this.take(Attr.RHO)).asNumber();
60+
final Double left = Expect.at(this, Attr.RHO)
61+
.that(phi -> new Dataized(phi).asNumber())
62+
.otherwise("must be a number")
63+
.it();
6164
final Double right = Expect.at(this, "x")
6265
.that(phi -> new Dataized(phi).asNumber())
66+
.otherwise("must be a number")
6367
.it();
6468
return new Data.ToPhi(left * right);
6569
}

eo-runtime/src/main/java/org/eolang/Expect.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,11 @@ public Expect<T> otherwise(final String message) {
114114
);
115115
} catch (final ExThat ex) {
116116
throw new ExFailure(
117-
message,
117+
String.format(
118+
"%s %s",
119+
this.subject,
120+
message
121+
),
118122
ex
119123
);
120124
}

eo-runtime/src/test/java/EOorg/EOeolang/EOnumber$EOtimesTest.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,30 @@
4848
final class EOnumber$EOtimesTest {
4949

5050
@Test
51-
void throwsCorrectError() {
51+
void throwsCorrectErrorWhenRhoAttrIsWrong() {
52+
MatcherAssert.assertThat(
53+
"the message in the error is correct",
54+
Assertions.assertThrows(
55+
ExAbstract.class,
56+
() -> new Dataized(
57+
new PhWith(
58+
new PhWith(
59+
new EOnumber$EOtimes(),
60+
Attr.RHO,
61+
new Data.ToPhi(true)
62+
),
63+
"x",
64+
new Data.ToPhi(42)
65+
)
66+
).take(),
67+
"Attr.RHO must be a number, not anything else"
68+
).getMessage(),
69+
Matchers.equalTo("the 'ρ' attribute must be a number")
70+
);
71+
}
72+
73+
@Test
74+
void throwsCorrectErrorWhenXAttrIsWrong() {
5275
MatcherAssert.assertThat(
5376
"the message in the error is correct",
5477
Assertions.assertThrows(
@@ -66,7 +89,7 @@ void throwsCorrectError() {
6689
).take(),
6790
"multiplies 3 by TRUE and fails with a proper message that explains what happened"
6891
).getMessage(),
69-
Matchers.containsString("number.times expects its second argument to be a number")
92+
Matchers.equalTo("the 'x' attribute must be a number")
7093
);
7194
}
7295
}

eo-runtime/src/test/java/org/eolang/ExpectTest.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,41 @@ void failsWithCorrectTraceWithExFailureInThat() {
107107
"Take error message from 'otherwise', not from original error",
108108
Assertions.assertThrows(
109109
ExFailure.class,
110-
() -> new Expect<>("something", () -> 42.2)
111-
.must(i -> i > 0)
112-
.otherwise("must be positive")
110+
() -> new Expect<>("attr", () -> 42.2)
113111
.that(
114112
i -> {
115-
throw new ExFailure("some error");
113+
throw new ExFailure("Some error in operation");
116114
}
117115
)
118-
.otherwise("something went wrong")
116+
.otherwise("must be converted to something")
117+
.it(),
118+
"fails on 'that' because of some internal error"
119+
).getMessage(),
120+
Matchers.equalTo("attr must be converted to something")
121+
);
122+
}
123+
124+
@Test
125+
void failsWithCorrectTraceWithExFailureInThatForParsing() {
126+
MatcherAssert.assertThat(
127+
"Take error message from 'otherwise', not from original error",
128+
Assertions.assertThrows(
129+
ExFailure.class,
130+
() -> new Expect<>("attr", () -> "string")
131+
.that(
132+
i -> {
133+
try {
134+
return Integer.parseInt(i);
135+
} catch (final NumberFormatException ex) {
136+
throw new ExFailure("Can't parse to integer", ex);
137+
}
138+
}
139+
)
140+
.otherwise("must be an integer")
119141
.it(),
120-
"fails on 'that'"
142+
"fails on 'that' because can not parse"
121143
).getMessage(),
122-
Matchers.equalTo("something went wrong")
144+
Matchers.equalTo("attr must be an integer")
123145
);
124146
}
125147
}

0 commit comments

Comments
 (0)