Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for vector syntaxes #6163

Merged
merged 19 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
@Description("Checks whether a vector is normalized i.e. length of 1")
@Examples("vector of player's location is normalized")
@Since("2.5.1")
@RequiredPlugins("Minecraft 1.13.2+")
public class CondIsVectorNormalized extends PropertyCondition<Vector> {

static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
public class ExprVectorCylindrical extends SimpleExpression<Vector> {

static {
Skript.registerExpression(ExprVectorCylindrical.class, Vector.class, ExpressionType.COMBINED,
"[a] [new] cylindrical vector [(from|with)] [radius] %number%, [yaw] %number%(,| and) [height] %number%");
Skript.registerExpression(ExprVectorCylindrical.class, Vector.class, ExpressionType.SIMPLE,
"[a] [new] cylindrical vector [from|with] [radius] %number%, [yaw] %number%(,[ and]| and) [height] %number%");
}

@SuppressWarnings("null")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
public class ExprVectorSpherical extends SimpleExpression<Vector> {

static {
Skript.registerExpression(ExprVectorSpherical.class, Vector.class, ExpressionType.COMBINED,
"[new] spherical vector [(from|with)] [radius] %number%, [yaw] %number%(,| and) [pitch] %number%");
Skript.registerExpression(ExprVectorSpherical.class, Vector.class, ExpressionType.SIMPLE,
"[a] [new] spherical vector [(from|with)] [radius] %number%, [yaw] %number%(,[ and]| and) [pitch] %number%");
}

@SuppressWarnings("null")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test "normalized vector":
assert vector(0,1,0) is normalized with "vector(0,1,0) not recognized as normal"
assert vector(1,0,0) is normalized with "vector(1,0,0) not recognized as normal"
assert vector(1,2,3) is not normalized with "vector(1,2,3) recognized as normal"
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
test "vector rotate around vector":
set {_x} to vector(1, 0, 0)
set {_y} to vector(0, 1, 0)
set {_z} to vector(0, 0, 1)

set {_v} to {_x}
rotate {_v} around {_y} by -90 degrees
assert {_v} is {_z} with "rotate around y vector failed (expected %{_z}%, got %{_v}%)"

rotate {_v} around {_x} by -90 degrees
assert {_v} is {_y} with "rotate around x vector failed (expected %{_y}%, got %{_v}%)"

rotate {_v} around {_z} by -90 degrees
assert {_v} is {_x} with "rotate around z vector failed (expected %{_x}%, got %{_v}%)"

rotate {_v} around vector(1, 1, 1) by 180 degrees
assert {_v} is vector(-1/3, 2/3, 2/3) with "rotate around non-orthogonal vector failed (expected %vector(-1/3, 2/3, 2/3)%, got %{_v}%)"

set {_v} to vector(2, 0, 0)
rotate {_v} around vector(0, 10, 0) by 90 degrees
assert {_v} is vector(0, 0, -2) with "rotate around non-unit vector failed (expected %vector(0, 0, -2)%, got %{_v}%)"
11 changes: 11 additions & 0 deletions src/test/skript/tests/syntaxes/effects/EffVectorRotateXYZ.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test "vector rotate around axis":

set {_v} to vector(1, 1, 1)
rotate {_v} around y axis by 90 degrees
assert {_v} is vector(1, 1, -1) with "rotate around y vector failed (expected %vector(1, 1, -1)%, got %{_v}%)"

rotate {_v} around x axis by -90 degrees
assert {_v} is vector(1, -1, -1) with "rotate around x vector failed (expected %vector(1, -1, -1)%, got %{_v}%)"

rotate {_v} around z axis by -90 degrees
assert {_v} is vector(-1, -1, -1) with "rotate around z vector failed (expected %vector(-1, -1, -1)%, got %{_v}%)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
test "location vector offset":
set {_location} to the spawn of world "world"
set {_offset} to {_location} offset by vector(0, 0, 0)
assert {_offset} is {_location} with "no offset equality check failed"
assert the distance between {_offset} and {_location} is 0 with "0 offset check failed"
set {_offset} to {_location} offset by vector(0, 1, 0)
assert the distance between {_offset} and {_location} is 1 with "1 offset check failed"
loop 60 times:
set {_x} to a random number between -100 and 100
set {_y} to a random number between -100 and 100
set {_z} to a random number between -100 and 100
set {_offset} to {_location} ~ vector({_x}, {_y}, {_z})
set {_length} to the normal length of vector({_x}, {_y}, {_z})
assert the distance between {_offset} and {_location} is {_length} with "randomly-created vector offset failed (expected %{_length}%, got %distance between {_offset} and {_location}%)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
test "angle between vectors":
set {_x} to vector(1,0,0)
set {_y} to vector(0,1,0)
assert angle between vectors {_x} and {_y} is 90 with "angle between x and y vectors is not 90"

rotate {_x} around {_y} by 90
assert angle between vectors {_x} and {_y} is 90 with "angle between vectors changed after rotation around one another"

set vector length of {_x} to 2
assert angle between vectors {_x} and {_y} is 90 with "angle between vectors changed after changing length of one"

assert angle between vector(1, 0, 0) and vector(-1, 0, 0) is 180 with "angle between opposite vectors is not 180"
assert angle between {_x} and {_x} is 0 with "angle between same vectors is not 0"

assert isNaN(angle between vector(0, 0, 0) and vector(0, 1, 0)) is true with "angle between zero vector is not NaN"

16 changes: 16 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprVectorArithmetic.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
test "vector arithmetic":
assert vector(0, 0, 0) + vector(1, 1, 1) is vector(1, 1, 1) with "vector addition failed (expected %vector(1, 1, 1)%, got %vector(0, 0, 0) ++ vector(1, 1, 1)%)"
assert vector(1, 1, 1) - vector(1, 1, 1) is vector(0, 0, 0) with "vector subtraction failed (expected %vector(0, 0, 0)%, got %vector(1, 1, 1) -- vector(1, 1, 1)%)"
assert vector(1, 1, 2) * vector(1, 2, 3) is vector(1, 2, 6) with "vector multiplication failed (expected %vector(1, 2, 6)%, got %vector(1, 1, 2) ** vector(1, 2, 3)%)"
assert vector(1, 2, 6) / vector(1, 2, 3) is vector(1, 1, 2) with "vector division failed (expected %vector(1, 1, 2)%, got %vector(1, 2, 6) // vector(1, 2, 3)%)"

set {_v} to vector(0, 0, 0) * random vector
assert {_v} is vector(0, 0, 0) with "zero vector multiplication failed (expected %vector(0, 0, 0)%, got %{_v}%)"

set {_v} to vector(1, 0, 1) / vector(0, 0, 0)
set {_x} to x component of {_v}
set {_y} to y component of {_v}
set {_z} to z component of {_v}
assert {_x} is infinity value with "division by zero failed (x component) (expected infinity, got %{_x}%)"
assert isNaN({_y}) is true with "division by zero failed (y component) (expected NaN, got %{_y}%"
assert {_z} is infinity value with "division by zero failed (z component) (expected infinity, got %{_z}%)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
test "vector between locations":
set {_world} to world("world")
set {_loc1} to location(0,0,0,{_world})
set {_loc2} to location(10,10,10,{_world})
set {_vec} to vector between {_loc1} and {_loc2}
assert {_vec} is vector(10,10,10) with "vector between locations in same world failed (expected %vector(10,10,10)%, got %{_vec}%)"

set {_world2} to world("world_the_end")
assert {_world2} is set with "no end world found"
set {_loc3} to location(10,10,10,{_world2})
set {_vec2} to vector between {_loc1} and {_loc3}
assert {_vec2} is vector(10,10,10) with "vector between locations in different worlds failed (expected %vector(10,10,10)%, got %{_vec2}%)"

set {_vec3} to vector between {_loc1} and {_loc1}
assert {_vec3} is vector(0,0,0) with "vector between same locations failed (expected %vector(0,0,0)%, got %{_vec3}%)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
test "cross product":
set {_v} to vector(1, 0, 0)
set {_w} to vector(0, 1, 0)
set {_u} to {_v} cross {_w}
assert {_u} is vector(0, 0, 1) with "cross product of basis vectors failed (expected %vector(0,0,1)%, got %{_u}%)"

set {_v} to vector(1, 1, 1)
set {_w} to vector(1, 1, -1)
set {_u} to {_v} cross {_w}
assert {_u} is vector(-2, 2, 0) with "cross product of non-basis vectors failed (expected %vector(-2, 2, 0)%, got %{_u}%)"

set {_v} to vector(1, 2, 3)
set {_w} to vector(1, 2, 3)
set {_u} to {_v} cross {_w}
assert {_u} is vector(0, 0, 0) with "cross product of parallel vectors failed (expected %vector(0, 0, 0)%, got %{_u}%)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test "cylindrical vector":
set {_v} to cylindrical vector with radius 1, yaw 0, and height 1
assert {_v} is vector(0, 1, 1) with "failed to create cylindrical vector (expected %vector(0, 1, 1)%, got %{_v}%)"

set {_v} to cylindrical vector with radius 1, yaw 45, and height 1
assert {_v} is vector(sqrt(2)/-2, 1, sqrt(2)/2) with "failed to create cylindrical vector (expected %vector(sqrt(2)/-2, 1, sqrt(2)/2)%, got %{_v}%)"
33 changes: 33 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprVectorDotProduct.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
test "cross product":
set {_v} to vector(1, 0, 0)
set {_w} to vector(0, 1, 0)
set {_u} to {_v} dot {_w}
assert {_u} is 0 with "dot product of orthogonal unit vectors failed (expected 0, got %{_u}%)"

set {_v} to vector(-5, 0, 0)
set {_w} to vector(0, 0, 10)
set {_u} to {_v} dot {_w}
assert {_u} is 0 with "dot product of orthogonal non-unit vectors failed (expected 0, got %{_u}%)"

set {_v} to vector(1, 0, 0)
set {_u} to {_v} dot {_v}
assert {_u} is 1 with "dot product of parallel unit vectors failed (expected 1, got %{_u}%)"

set {_v} to vector(1, 1, 1)
set {_u} to {_v} dot {_v}
assert {_u} is 3 with "dot product of parallel non-unit vectors failed (expected 3, got %{_u}%)"

set {_v} to vector(1, 0, 0)
set {_w} to vector(-1, 0, 0)
set {_u} to {_v} dot {_w}
assert {_u} is -1 with "dot product of anti-parallel unit vectors failed (expected -1, got %{_u}%)"

set {_v} to vector(1, 1, 1)
set {_w} to vector(-1, -1, -1)
set {_u} to {_v} dot {_w}
assert {_u} is -3 with "dot product of anti-parallel non-unit vectors failed (expected -3, got %{_u}%)"

set {_v} to vector(1, 0, 0)
set {_w} to vector(0, 0, 0)
set {_u} to {_v} dot {_w}
assert {_u} is 0 with "dot product of zero vector failed (expected 0, got %{_u}%)"
11 changes: 11 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprVectorFromXYZ.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test "vector from xyz":
assert (a new vector to 0, 0 and 0) is vector(0, 0, 0) with "zero vector creation failed"
loop 60 times:
set {_x} to a random number between -100 and 100
set {_y} to a random number between -100 and 100
set {_z} to a random number between -100 and 100
set {_vector} to a new vector from {_x}, {_y}, {_z}
assert {_vector} is vector({_x}, {_y}, {_z}) with "randomly-created vector equality failed (expected %vector({_x}, {_y}, {_z})%, got %{_vector}%)"
set {_random} to a random vector
set {_vector} to a new vector from x component of {_random}, y component of {_random}, z component of {_random}
assert {_vector} is {_random} with "random vector to created component equality failed (expected %{_random}%, got %{_vector}%)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
test "vector from yaw and pitch":
assert (a new vector from yaw 0 and pitch 0) is vector(0, 0, 1) with "zero-angle vector creation failed: %a new vector from yaw 0 and pitch 0%"
assert (a new vector from yaw 90 and pitch 0) is vector(-1, 0, 0) with "90-yaw vector creation failed: %a new vector from yaw 90 and pitch 0%"
assert (a new vector from yaw 180 and pitch 0) is vector(0, 0, -1) with "180-yaw vector creation failed: %a new vector from yaw 180 and pitch 0%"
assert (a new vector from yaw -90 and pitch 0) is vector(1, 0, 0) with "-90-yaw vector creation failed: %a new vector from yaw -90 and pitch 0%"
assert (a new vector from yaw 0 and pitch 90) is vector(0, -1, 0) with "90-pitch vector creation failed: %a new vector from yaw 0 and pitch 90%"
assert (a new vector from yaw 0 and pitch -90) is vector(0, 1, 0) with "-90-pitch vector creation failed: %a new vector from yaw 0 and pitch -90%"
loop 60 times:
set {_yaw} to a random number between -180 and 180
set {_pitch} to a random number between -90 and 90
set {_vector} to a new vector from yaw {_yaw} and pitch {_pitch}
assert the normal length of {_vector} is 1 with "randomly-created vector was non-normal (expected length 1, got %normal length of {_vector}%)"
11 changes: 11 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprVectorLength.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test "vector length":
assert the normal length of vector(0, 0, 0) is 0 with "0-length vector check failed"
assert the standard length of vector(0, 1, 0) is 1 with "1-length vector check failed"
assert the vector length of vector(0, 0, 2) is 2 with "2-length vector check failed"
loop 60 times:
set {_x} to a random number between -100 and 100
set {_y} to a random number between -100 and 100
set {_z} to a random number between -100 and 100
set {_vector} to vector({_x}, {_y}, {_z})
set {_length} to sqrt(({_x} ^ 2) + ({_y} ^ 2) + ({_z} ^ 2))
assert the normal length of {_vector} is {_length} with "randomly-created vector length failed (expected %{_length}%, got %normal length of {_vector}%)"
11 changes: 11 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprVectorNormalize.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test "vector normalize":
assert the standard length of vector(0, 1, 0) normalized is 1 with "1-length vector normalisation check failed"
assert the vector length of vector(0, 0, 2) normalized is 1 with "2-length vector normalisation check failed"
assert vector(0, 0, 2) normalized is vector(0, 0, 1) with "normalised vector component comparison failed"
assert vector(6, 6, 6) normalized is vector(1, 1, 1) normalized with "normalised vector comparison failed"
loop 60 times:
set {_x} to a random number between -100 and 100
set {_y} to a random number between -100 and 100
set {_z} to a random number between -100 and 100
set {_vector} to vector({_x}, {_y}, {_z}) normalized
assert the normal length of {_vector} is 1 with "randomly-created vector normalisation failed (expected 1, got %normal length of {_vector}%)"
22 changes: 22 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprVectorOfLocation.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
test "vector of location":
set {_loc} to location(1,0,0,world "world")
set {_v} to vector of {_loc}
assert x component of {_v} is x coordinate of {_loc} with "vector/location x equality failed (expected %x coordinate of {_loc}%, got %x component of {_v}%)"
assert y component of {_v} is y coordinate of {_loc} with "vector/location y equality failed (expected %y coordinate of {_loc}%, got %y component of {_v}%)"
assert z component of {_v} is z coordinate of {_loc} with "vector/location z equality failed (expected %z coordinate of {_loc}%, got %z component of {_v}%)"

set {_loc} to location(1,2,3,world "invalid world")
set {_v} to vector of {_loc}
assert x component of {_v} is x coordinate of {_loc} with "vector/location x equality failed when world is null (expected %x coordinate of {_loc}%, got %x component of {_v}%)"
assert y component of {_v} is y coordinate of {_loc} with "vector/location y equality failed when world is null (expected %y coordinate of {_loc}%, got %y component of {_v}%)"
assert z component of {_v} is z coordinate of {_loc} with "vector/location z equality failed when world is null (expected %z coordinate of {_loc}%, got %z component of {_v}%)"


set {_x} to random number between -10000 and 10000
set {_y} to random number between -10000 and 10000
set {_z} to random number between -10000 and 10000
set {_loc} to location({_x}, {_y}, {_z},world "world")
set {_v} to vector of {_loc}
assert x component of {_v} is x coordinate of {_loc} with "random vector/location x equality failed (expected %x coordinate of {_loc}%, got %x component of {_v}%)"
assert y component of {_v} is y coordinate of {_loc} with "random vector/location y equality failed (expected %y coordinate of {_loc}%, got %y component of {_v}%)"
assert z component of {_v} is z coordinate of {_loc} with "random vector/location z equality failed (expected %z coordinate of {_loc}%, got %z component of {_v}%)"
11 changes: 11 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprVectorRandom.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test "vector random":
loop 100 times:
set {_vector} to a random vector
assert {_vector} is set with "random vector creation failed"
assert {_vector} is not vector(0, 0, 0) with "random vector was non-normal zero"
assert x component of {_vector} is greater than or equal to -1 with "random vector x was too small"
assert y component of {_vector} is greater than or equal to -1 with "random vector y was too small"
assert z component of {_vector} is greater than or equal to -1 with "random vector z was too small"
assert x component of {_vector} is less than or equal to 1 with "random vector x was too large"
assert y component of {_vector} is less than or equal to 1 with "random vector y was too large"
assert z component of {_vector} is less than or equal to 1 with "random vector z was too large"
17 changes: 17 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprVectorSpherical.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
test "vector spherical":
set {_vector} to a new spherical vector 0, 0, 0
assert the normal length of {_vector} is 0 with "zero-length vector had non-zero length"
assert {_vector} is vector(0, 0, 0) with "zero spherical vector was not zero vector"
loop 10 times:
set {_yaw} to a random number between -180 and 180
set {_pitch} to a random number between -90 and 90
set {_vector} to a new spherical vector with radius 0, yaw {_yaw} and pitch {_pitch}
assert the normal length of {_vector} is 0 with "random zero-length vector had non-zero length"
assert {_vector} is vector(0, 0, 0) with "random zero spherical vector was not zero vector"
assert (a new spherical vector 2, 0, -90) is vector(0, 2, 0) with "upwards 2-length vector equality failed"
assert (a new spherical vector 1, 0, 0) is vector(0, 0, 1) with "zero-angle vector creation failed: %a new vector from yaw 0 and pitch 0%"
assert (a new spherical vector 1, 90, 0) is vector(-1, 0, 0) with "90-yaw vector creation failed: %a new vector from yaw 90 and pitch 0%"
assert (a new spherical vector 1, 180, 0) is vector(0, 0, -1) with "180-yaw vector creation failed: %a new vector from yaw 180 and pitch 0%"
assert (a new spherical vector 1, -90, 0) is vector(1, 0, 0) with "-90-yaw vector creation failed: %a new vector from yaw -90 and pitch 0%"
assert (a new spherical vector 1, 0, 90) is vector(0, -1, 0) with "90-pitch vector creation failed: %a new vector from yaw 0 and pitch 90%"
assert (a new spherical vector 1, 0, -90) is vector(0, 1, 0) with "-90-pitch vector creation failed: %a new vector from yaw 0 and pitch -90%"
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test "vector squared length":
assert the squared length of vector(0, 0, 0) is 0 with "0-length vector check failed"
assert the squared length of vector(0, 1, 0) is 1 with "1-length vector check failed"
assert the squared length of vector(0, 0, 2) is 4 with "2-length vector check failed"
loop 60 times:
set {_x} to a random number between -100 and 100
set {_y} to a random number between -100 and 100
set {_z} to a random number between -100 and 100
set {_vector} to vector({_x}, {_y}, {_z})
set {_length} to ({_x} ^ 2) + ({_y} ^ 2) + ({_z} ^ 2)
assert the squared length of {_vector} is {_length} with "randomly-created vector length failed (expected %{_length}%, got %squared length of {_vector}%)"
16 changes: 16 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprVectorXYZ.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
test "vector xyz":
assert the x component of vector(0, 0, 0) is 0 with "x = 0 vector component failed"
assert the x component of vector(1, 0, 0) is 1 with "x = 1 vector component failed"
assert the y component of vector(1, 1, 0) is 1 with "y = 1 vector component failed"
assert the z component of vector(1, 1, 1) is 1 with "z = 1 vector component failed"
loop 60 times:
set {_x} to a random number between -100 and 100
set {_y} to a random number between -100 and 100
set {_z} to a random number between -100 and 100
set {_vector} to vector({_x}, {_y}, {_z})
assert the x component of {_vector} is {_x} with "randomly-created vector x equality failed (expected %{_x}%, got %x component of {_vector}%)"
assert the y component of {_vector} is {_y} with "randomly-created vector y equality failed (expected %{_y}%, got %y component of {_vector}%)"
assert the z component of {_vector} is {_z} with "randomly-created vector z equality failed (expected %{_z}%, got %z component of {_vector}%)"
set {_random} to a random vector
set {_vector} to a new vector from x component of {_random}, y component of {_random}, z component of {_random}
assert {_vector} is {_random} with "random vector to created component equality failed (expected %{_random}%, got %{_vector}%)"
Loading