Skip to content

Commit

Permalink
test: add generate-mixed-colors test
Browse files Browse the repository at this point in the history
  • Loading branch information
red-freak committed Aug 26, 2023
1 parent 71c0492 commit 4a44a34
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
24 changes: 22 additions & 2 deletions functions/_colors.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
@use "sass:math";
@use "sass:string";

@use './type-checking' as check;

@use '../node_modules/sass-true/sass/throw' as throw;
$_is-test: false !default;

/// Convert HEX colors to R, G, B values
/// @param {Color} $color
/// @return {String}
Expand Down Expand Up @@ -106,11 +111,26 @@
$steps: 20,
$color2-peak: 11,
) {
@if(check.is-color($color1) == false) {
@return throw.error("❌ ===> The color1 parameter must be a color.", $catch: $_is-test);
}
@if(check.is-color($color2) == false) {
@return throw.error("❌ ===> The color2 parameter must be a color.", $catch: $_is-test);
}
@if(check.is-color($color3) == false) {
@return throw.error("❌ ===> The color3 parameter must be a color.", $catch: $_is-test);
}
@if(check.is-integer($steps) == false) {
@return throw.error("❌ ===> The steps parameter must be an integer.", $catch: $_is-test);
}
@if(check.is-integer($color2-peak) == false) {
@return throw.error("❌ ===> The color2-peak parameter must be an integer.", $catch: $_is-test);
}
@if ($color2-peak > $steps) {
@error "The peak point for color2 must be less than or equal to the number of steps.";
@return throw.error("❌ ===> The peak point for color2 must be less than or equal to the number of steps.", $catch: $_is-test);
}
@if ($color2-peak < 1) {
@error "The peak point for color2 must be greater than or equal to 1.";
@return throw.error("❌ ===> The peak point for color2 must be greater than or equal to 1.", $catch: $_is-test);
}

$colorMap: (1: $color1);
Expand Down
9 changes: 9 additions & 0 deletions functions/_type-checking.scss
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,12 @@
@return is-length($value) or is-percentage($value) or
list.index('top' 'right' 'bottom' 'left' 'center', $value) != null;
}

/// Check if its a color value
/// @param {Void} $value - Value to check
/// @group Type-Checking
/// @author https://github.com/red-freak
/// @since v2.5.0
@function is-color($value) {
@return meta.type-of($value) == 'color';
}
48 changes: 48 additions & 0 deletions tests/colors-generate-mixed-colors.spec.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//@use 'true';
$_is-test: true;
@use '../node_modules/sass-true/sass/true' as true;
@import '../functions/_colors.scss';


@include true.describe('generate-mixed-colors()') {
@include true.it('should throw an error if the step and peak parameters are wrong') {
@include true.assert-equal(
generate-mixed-colors(1, 1, 1), 'ERROR: ❌ ===> The color1 parameter must be a color.'
);
@include true.assert-equal(
generate-mixed-colors(green, 1, 1), 'ERROR: ❌ ===> The color2 parameter must be a color.'
);
@include true.assert-equal(
generate-mixed-colors(green, yellow, 1), 'ERROR: ❌ ===> The color3 parameter must be a color.'
);
@include true.assert-equal(
generate-mixed-colors(green, yellow, red, "hallo"), 'ERROR: ❌ ===> The steps parameter must be an integer.'
);
@include true.assert-equal(
generate-mixed-colors(green, yellow, red, 10, "welt"), 'ERROR: ❌ ===> The color2-peak parameter must be an integer.'
);
@include true.assert-equal(
generate-mixed-colors(green, yellow, red, 0, 2), 'ERROR: ❌ ===> The peak point for color2 must be less than or equal to the number of steps.'
);
@include true.assert-equal(
generate-mixed-colors(green, yellow, red, 0, 0), 'ERROR: ❌ ===> The peak point for color2 must be greater than or equal to 1.'
);
@include true.assert-equal(
generate-mixed-colors(green, yellow, red, -1, -1), 'ERROR: ❌ ===> The peak point for color2 must be greater than or equal to 1.'
);
}
@include true.it('should return expected color maps') {
@include true.assert-equal(
generate-mixed-colors(green, yellow, red, 1, 1), (1: yellow)
);
@include true.assert-equal(
generate-mixed-colors(green, yellow, red, 3, 2), (1: green, 2: yellow, 3: red)
);
@include true.assert-equal(
generate-mixed-colors(green, yellow, red, 2, 1), (1: yellow, 2: red)
);
@include true.assert-equal(
generate-mixed-colors(green, yellow, red), (1: green, 2: #2e9700, 3: #46a300, 4: #5dae00, 5: #74ba00, 6: #8bc500, 7: #a2d100, 8: #b9dc00, 9: #d1e800, 10: #e8f300, 11: yellow, 12: #ffe300, 13: #ffc600, 14: #ffaa00, 15: #ff8e00, 16: #ff7100, 17: #ff5500, 18: #ff3900, 19: #ff1c00, 20: red)
);
}
}

0 comments on commit 4a44a34

Please sign in to comment.