Skip to content

Commit

Permalink
added option instead of flags
Browse files Browse the repository at this point in the history
Signed-off-by: Nishant Bansal <[email protected]>
  • Loading branch information
NishantBansal2003 committed Nov 1, 2024
1 parent a9bef44 commit 63b990a
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 50 deletions.
41 changes: 24 additions & 17 deletions raster/r.colors.out/raster3d_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ int main(int argc, char **argv)
{
struct GModule *module;
struct {
struct Option *map, *file, *format;
struct Option *map, *file, *format, *color_format;
} opt;
struct {
struct Flag *p, *r, *x, *h;
struct Flag *p;
} flag;

const char *file;
Expand Down Expand Up @@ -63,22 +63,29 @@ int main(int argc, char **argv)
opt.format = G_define_standard_option(G_OPT_F_FORMAT);
opt.format->guisection = _("Print");

opt.color_format = G_define_option();
opt.color_format->key = "color_format";
opt.color_format->type = TYPE_STRING;
opt.color_format->key_desc = "name";
opt.color_format->required = YES;
opt.color_format->multiple = NO;
opt.color_format->answer = "xterm";
opt.color_format->options = "rgb,hex,hsv,xterm";
opt.color_format->label = _("Color format");
opt.color_format->description =
_("Color format output for raster3d values.");
char *desc = NULL;
G_asprintf(&desc, "rgb;%s;hex;%s;hsv;%s;xterm;%s",
_("output color in RGB format"), _("output color in HEX format"),
_("output color in HSV format"),
_("output color in XTERM format"));
opt.color_format->descriptions = desc;
opt.color_format->guisection = _("Color");

flag.p = G_define_flag();
flag.p->key = 'p';
flag.p->description = _("Output values as percentages");

flag.r = G_define_flag();
flag.r->key = 'r';
flag.r->description = _("Output colors as rgb format");

flag.x = G_define_flag();
flag.x->key = 'x';
flag.x->description = _("Output colors as hex format");

flag.h = G_define_flag();
flag.h->key = 'h';
flag.h->description = _("Output colors as hsv format");

if (G_parser(argc, argv))
exit(EXIT_FAILURE);

Expand All @@ -99,13 +106,13 @@ int main(int argc, char **argv)
}

if (strcmp(opt.format->answer, "json") == 0) {
if (flag.r->answer) {
if (strcmp(opt.color_format->answer, "rgb") == 0) {
clr_frmt = RGB;
}
else if (flag.x->answer) {
else if (strcmp(opt.color_format->answer, "hex") == 0) {
clr_frmt = HEX;
}
else if (flag.h->answer) {
else if (strcmp(opt.color_format->answer, "hsv") == 0) {
clr_frmt = HSV;
}
else {
Expand Down
40 changes: 23 additions & 17 deletions raster/r.colors.out/raster_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ int main(int argc, char **argv)
{
struct GModule *module;
struct {
struct Option *map, *file, *format;
struct Option *map, *file, *format, *color_format;
} opt;
struct {
struct Flag *p, *r, *x, *h;
struct Flag *p;
} flag;

const char *file;
Expand Down Expand Up @@ -62,22 +62,28 @@ int main(int argc, char **argv)
opt.format = G_define_standard_option(G_OPT_F_FORMAT);
opt.format->guisection = _("Print");

opt.color_format = G_define_option();
opt.color_format->key = "color_format";
opt.color_format->type = TYPE_STRING;
opt.color_format->key_desc = "name";
opt.color_format->required = YES;
opt.color_format->multiple = NO;
opt.color_format->answer = "xterm";
opt.color_format->options = "rgb,hex,hsv,xterm";
opt.color_format->label = _("Color format");
opt.color_format->description = _("Color format output for raster values.");
char *desc = NULL;
G_asprintf(&desc, "rgb;%s;hex;%s;hsv;%s;xterm;%s",
_("output color in RGB format"), _("output color in HEX format"),
_("output color in HSV format"),
_("output color in XTERM format"));
opt.color_format->descriptions = desc;
opt.color_format->guisection = _("Color");

flag.p = G_define_flag();
flag.p->key = 'p';
flag.p->description = _("Output values as percentages");

flag.r = G_define_flag();
flag.r->key = 'r';
flag.r->description = _("Output colors as rgb format");

flag.x = G_define_flag();
flag.x->key = 'x';
flag.x->description = _("Output colors as hex format");

flag.h = G_define_flag();
flag.h->key = 'h';
flag.h->description = _("Output colors as hsv format");

if (G_parser(argc, argv))
exit(EXIT_FAILURE);

Expand All @@ -98,13 +104,13 @@ int main(int argc, char **argv)
}

if (strcmp(opt.format->answer, "json") == 0) {
if (flag.r->answer) {
if (strcmp(opt.color_format->answer, "rgb") == 0) {
clr_frmt = RGB;
}
else if (flag.x->answer) {
else if (strcmp(opt.color_format->answer, "hex") == 0) {
clr_frmt = HEX;
}
else if (flag.h->answer) {
else if (strcmp(opt.color_format->answer, "hsv") == 0) {
clr_frmt = HSV;
}
else {
Expand Down
36 changes: 28 additions & 8 deletions raster/r.colors.out/tests/r3_colors_out_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ def validate_common_json_structure(data):
), "The length of the output JSON does not match the expected value of 8."


def test_r3_colors_out_json_with_default_flag(raster3_color_dataset):
"""Test r3.colors.out command for JSON output format for xterm color option."""
def test_r3_colors_out_json_with_default_option(raster3_color_dataset):
"""Test r3.colors.out command for JSON output format for default color option."""
session = raster3_color_dataset
data = gs.parse_command("r3.colors.out", map="b", format="json", env=session.env)
validate_common_json_structure(data)
Expand All @@ -83,14 +83,34 @@ def test_r3_colors_out_json_with_default_flag(raster3_color_dataset):
assert expected == data, f"test failed: expected {expected} but got {data}"


def test_r3_colors_out_json_with_rgb_flag(raster3_color_dataset):
def test_r3_colors_out_json_with_xterm_option(raster3_color_dataset):
"""Test r3.colors.out command for JSON output format for xterm color option."""
session = raster3_color_dataset
data = gs.parse_command(
"r3.colors.out", map="b", format="json", color_format="xterm", env=session.env
)
validate_common_json_structure(data)
expected = [
{"value": 1, "RGB": "0:191:191"},
{"value": 1.8, "RGB": "0:255:0"},
{"value": 2.6, "RGB": "255:255:0"},
{"value": 3.4, "RGB": "255:127:0"},
{"value": 4.2, "RGB": "191:127:63"},
{"value": 5, "RGB": "200:200:200"},
{"value": "nv", "RGB": "255:255:255"},
{"value": "default", "RGB": "255:255:255"},
]
assert expected == data, f"test failed: expected {expected} but got {data}"


def test_r3_colors_out_json_with_rgb_option(raster3_color_dataset):
"""Test r3.colors.out command for JSON output format for rgb color option."""
session = raster3_color_dataset
data = gs.parse_command(
"r3.colors.out",
map="b",
format="json",
flags="r",
color_format="rgb",
env=session.env,
)
validate_common_json_structure(data)
Expand All @@ -107,14 +127,14 @@ def test_r3_colors_out_json_with_rgb_flag(raster3_color_dataset):
assert expected == data, f"test failed: expected {expected} but got {data}"


def test_r3_colors_out_json_with_hex_flag(raster3_color_dataset):
def test_r3_colors_out_json_with_hex_option(raster3_color_dataset):
"""Test r3.colors.out command for JSON output format for hex color option."""
session = raster3_color_dataset
data = gs.parse_command(
"r3.colors.out",
map="b",
format="json",
flags="x",
color_format="hex",
env=session.env,
)
validate_common_json_structure(data)
Expand All @@ -131,14 +151,14 @@ def test_r3_colors_out_json_with_hex_flag(raster3_color_dataset):
assert expected == data, f"test failed: expected {expected} but got {data}"


def test_r3_colors_out_json_with_hsv_flag(raster3_color_dataset):
def test_r3_colors_out_json_with_hsv_option(raster3_color_dataset):
"""Test r3.colors.out command for JSON output format for hsv color option."""
session = raster3_color_dataset
data = gs.parse_command(
"r3.colors.out",
map="b",
format="json",
flags="h",
color_format="hsv",
env=session.env,
)
validate_common_json_structure(data)
Expand Down
36 changes: 28 additions & 8 deletions raster/r.colors.out/tests/r_colors_out_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ def validate_common_json_structure(data):
), "The length of the output JSON does not match the expected value of 8."


def test_r_colors_out_json_with_default_flag(raster_color_dataset):
"""Test r.colors.out command for JSON output format for xterm color option."""
def test_r_colors_out_json_with_default_option(raster_color_dataset):
"""Test r.colors.out command for JSON output format for default color option."""
session = raster_color_dataset
data = gs.parse_command("r.colors.out", map="a", format="json", env=session.env)
validate_common_json_structure(data)
Expand All @@ -83,14 +83,34 @@ def test_r_colors_out_json_with_default_flag(raster_color_dataset):
assert expected == data, f"test failed: expected {expected} but got {data}"


def test_r_colors_out_json_with_rgb_flag(raster_color_dataset):
def test_r_colors_out_json_with_xterm_option(raster_color_dataset):
"""Test r.colors.out command for JSON output format for xterm color option."""
session = raster_color_dataset
data = gs.parse_command(
"r.colors.out", map="a", format="json", color_format="xterm", env=session.env
)
validate_common_json_structure(data)
expected = [
{"value": 1, "RGB": "0:191:191"},
{"value": 1.4, "RGB": "0:255:0"},
{"value": 1.8, "RGB": "255:255:0"},
{"value": 2.2, "RGB": "255:127:0"},
{"value": 2.6, "RGB": "191:127:63"},
{"value": 3, "RGB": "200:200:200"},
{"value": "nv", "RGB": "255:255:255"},
{"value": "default", "RGB": "255:255:255"},
]
assert expected == data, f"test failed: expected {expected} but got {data}"


def test_r_colors_out_json_with_rgb_option(raster_color_dataset):
"""Test r.colors.out command for JSON output format for rgb color option."""
session = raster_color_dataset
data = gs.parse_command(
"r.colors.out",
map="a",
format="json",
flags="r",
color_format="rgb",
env=session.env,
)
validate_common_json_structure(data)
Expand All @@ -107,14 +127,14 @@ def test_r_colors_out_json_with_rgb_flag(raster_color_dataset):
assert expected == data, f"test failed: expected {expected} but got {data}"


def test_r_colors_out_json_with_hex_flag(raster_color_dataset):
def test_r_colors_out_json_with_hex_option(raster_color_dataset):
"""Test r.colors.out command for JSON output format for hex color option."""
session = raster_color_dataset
data = gs.parse_command(
"r.colors.out",
map="a",
format="json",
flags="x",
color_format="hex",
env=session.env,
)
validate_common_json_structure(data)
Expand All @@ -131,14 +151,14 @@ def test_r_colors_out_json_with_hex_flag(raster_color_dataset):
assert expected == data, f"test failed: expected {expected} but got {data}"


def test_r_colors_out_json_with_hsv_flag(raster_color_dataset):
def test_r_colors_out_json_with_hsv_option(raster_color_dataset):
"""Test r.colors.out command for JSON output format for hsv color option."""
session = raster_color_dataset
data = gs.parse_command(
"r.colors.out",
map="a",
format="json",
flags="h",
color_format="hsv",
env=session.env,
)
validate_common_json_structure(data)
Expand Down

0 comments on commit 63b990a

Please sign in to comment.