#include <gtest/gtest.h>

#include "grbl.h"
#include "grbl_machine.h"

TEST(grbl_program, default_state) {
    grbl::program pgm;

    EXPECT_EQ("", pgm.filename);
    EXPECT_EQ(0, pgm.number_of_instructions());
    EXPECT_EQ(false, pgm.is_loaded);
}

TEST(grbl_program, load_from_string) {
    grbl::program pgm;

    std::string content = R"(( pcb2gcode 1.2.2 )
( Software-independent Gcode )

G94 ( Millimeters per minute feed rate. )
G21 ( Units == Millimeters. )
F600.00000 ; another type of comment
X3.87739 Y78.52820
)";
    pgm.load_from_string(content);

    EXPECT_EQ("", pgm.filename);
    EXPECT_EQ(6, pgm.number_of_instructions());
    EXPECT_EQ(true, pgm.is_loaded);
}

TEST(grbl_status_report, parse) {
    auto content = "<Idle|MPos:0.000,1.000,2.000|Bf:35,1022|FS:0,0|Pn:Z|Ov:100,100,100>";
    grbl::realtime_status_report r;
    grbl::parse_status_report(content, r);

    EXPECT_EQ(grbl::machine_status::idle, r.status);
    EXPECT_EQ("", r.sub_status);

    EXPECT_EQ(0, r.machine_pos[0]);
    EXPECT_EQ(1, r.machine_pos[1]);
    EXPECT_EQ(2, r.machine_pos[2]);

    EXPECT_EQ(35, r.buffers_free);

    //
    grbl::parse_status_report("<Home|MPos:0.000,0.000,0.000|Bf:35,1022|FS:0,0|Pn:Z|Ov:100,100,100>", r);
    EXPECT_EQ(grbl::machine_status::home, r.status);
    EXPECT_EQ(1022, r.rx_chars_free);
}