File: GCodeExportTest.cpp

package info (click to toggle)
cura-engine 1%3A5.0.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,860 kB
  • sloc: cpp: 52,613; python: 322; makefile: 10; sh: 2
file content (697 lines) | stat: -rw-r--r-- 30,128 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
//Copyright (c) 2021 Ultimaker B.V.
//CuraEngine is released under the terms of the AGPLv3 or higher.

#include <gtest/gtest.h>

#include "../src/settings/types/LayerIndex.h"
#include "../src/utils/Date.h" //To check the Griffin header.
#include "../src/Application.h" //To set up a slice with settings.
#include "../src/gcodeExport.h" //The unit under test.
#include "../src/Slice.h" //To set up a slice with settings.
#include "../src/RetractionConfig.h" //For extruder switch tests.
#include "../src/WipeScriptConfig.h" //For wipe sciprt tests.
#include "arcus/MockCommunication.h" //To prevent calls to any missing Communication class.

namespace cura
{

/*
 * Fixture that provides a GCodeExport instance in a certain base state.
 */
class GCodeExportTest : public testing::Test
{
public:
    /*
     * An export class to test with.
     */
    GCodeExport gcode;

    /*
     * A stream to capture the output of the g-code export.
     */
    std::stringstream output;

    /*
     * Mock away the communication channel where layer data is output by this
     * class.
     */
    MockCommunication* mock_communication;

    void SetUp()
    {
        output << std::fixed;
        gcode.output_stream = &output;

        //Since GCodeExport doesn't support copying, we have to reset everything in-place.
        gcode.currentPosition = Point3(0, 0, MM2INT(20));
        gcode.layer_nr = 0;
        gcode.current_e_value = 0;
        gcode.current_e_offset = 0;
        gcode.current_extruder = 0;
        gcode.current_fan_speed = -1;
        gcode.total_print_times = std::vector<Duration>(static_cast<unsigned char>(PrintFeatureType::NumPrintFeatureTypes), 0.0);
        gcode.currentSpeed = 1;
        gcode.current_print_acceleration = -1;
        gcode.current_travel_acceleration = -1;
        gcode.current_jerk = -1;
        gcode.is_z_hopped = 0;
        gcode.setFlavor(EGCodeFlavor::MARLIN);
        gcode.bed_temperature = 0;
        gcode.initial_bed_temp = 0;
        gcode.fan_number = 0;
        gcode.total_bounding_box = AABB3D();
        gcode.current_layer_z = 0;
        gcode.relative_extrusion = false;

        gcode.new_line = "\n"; //Not BFB flavour by default.
        gcode.machine_name = "Your favourite 3D printer";
        gcode.machine_buildplate_type = "Your favourite build plate";

        //Set up a scene so that we may request settings.
        Application::getInstance().current_slice = new Slice(1);
        mock_communication = new MockCommunication();
        Application::getInstance().communication = mock_communication;
    }

    void TearDown()
    {
        delete Application::getInstance().current_slice;
        delete Application::getInstance().communication;
        Application::getInstance().communication = nullptr;
    }
};

TEST_F(GCodeExportTest, CommentEmpty)
{
    gcode.writeComment("");
    EXPECT_EQ(std::string(";\n"), output.str()) << "Semicolon and newline must exist but it must be empty for the rest.";
}

TEST_F(GCodeExportTest, CommentSimple)
{
    gcode.writeComment("extrude harder");
    EXPECT_EQ(std::string(";extrude harder\n"), output.str()) << "Message must be preceded by a semicolon and ends with a newline..";
}

TEST_F(GCodeExportTest, CommentMultiLine)
{
    gcode.writeComment("If you catch a chinchilla in Chile\n"
        "And cut off its beard, willy-nilly\n"
        "You can honestly say\n"
        "You made on that day\n"
        "A Chilean chinchilla's chin chilly");
    EXPECT_EQ(std::string(";If you catch a chinchilla in Chile\n"
        ";And cut off its beard, willy-nilly\n"
        ";You can honestly say\n"
        ";You made on that day\n"
        ";A Chilean chinchilla's chin chilly\n"), output.str()) << "Each line must be preceded by a semicolon.";
}

TEST_F(GCodeExportTest, CommentMultiple)
{
    gcode.writeComment("Thunderbolt and lightning");
    gcode.writeComment("Very very frightening me");
    gcode.writeComment(" - Galileo (1638)");
    EXPECT_EQ(std::string(";Thunderbolt and lightning\n"
        ";Very very frightening me\n"
        "; - Galileo (1638)\n"), output.str()) << "Semicolon before each line, and newline in between.";
}

TEST_F(GCodeExportTest, CommentTimeZero)
{
    gcode.writeTimeComment(0);
    EXPECT_EQ(std::string(";TIME_ELAPSED:0.000000\n"), output.str());
}

TEST_F(GCodeExportTest, CommentTimeInteger)
{
    gcode.writeTimeComment(42);
    EXPECT_EQ(std::string(";TIME_ELAPSED:42.000000\n"), output.str()) << "The time must be fixed-radix to the microsecond.";
}

TEST_F(GCodeExportTest, CommentTimeFloatRoundingError)
{
    gcode.writeTimeComment(0.3);
    EXPECT_EQ(std::string(";TIME_ELAPSED:0.300000\n"), output.str()) << "Don't output up to the precision of rounding errors.";
}

TEST_F(GCodeExportTest, CommentTypeAllTypesCovered)
{
    for (PrintFeatureType type = PrintFeatureType(0); type < PrintFeatureType::NumPrintFeatureTypes; type = PrintFeatureType(static_cast<size_t>(type) + 1))
    {
        gcode.writeTypeComment(type);
        if (type == PrintFeatureType::MoveCombing || type == PrintFeatureType::MoveRetraction)
        {
            EXPECT_EQ(std::string(""), output.str()) << "Travel moves shouldn't output a type.";
        }
        else if (type == PrintFeatureType::NoneType)
        {
            EXPECT_EQ(std::string(""), output.str()) << "NoneType shouldn't output a type.";
        }
        else
        {
            EXPECT_EQ(std::string(";TYPE:"), output.str().substr(0, 6)) << "Type " << static_cast<size_t>(type) << " is not implemented.";
        }
        output.str(""); //Reset so that our next measurement is clean again.
        output << std::fixed;
    }
}

TEST_F(GCodeExportTest, CommentLayer)
{
    gcode.writeLayerComment(9);
    EXPECT_EQ(std::string(";LAYER:9\n"), output.str()) << "Put the correct prefix and a newline afterwards.";
}

TEST_F(GCodeExportTest, CommentLayerNegative)
{
    gcode.writeLayerComment(-3);
    EXPECT_EQ(std::string(";LAYER:-3\n"), output.str());
}

TEST_F(GCodeExportTest, CommentLayerCount)
{
    gcode.writeLayerCountComment(5);
    EXPECT_EQ(std::string(";LAYER_COUNT:5\n"), output.str());
}

/*
 * Parameterized test with different numbers of extruders.
 */
class GriffinHeaderTest : public testing::TestWithParam<size_t>
{
public:
    /*
     * An export class to test with.
     */
    GCodeExport gcode;

    /*
     * A stream to capture the output of the g-code export.
     */
    std::stringstream output;

    void SetUp()
    {
        output << std::fixed;
        gcode.output_stream = &output;

        //Since GCodeExport doesn't support copying, we have to reset everything in-place.
        gcode.currentPosition = Point3(0, 0, MM2INT(20));
        gcode.layer_nr = 0;
        gcode.current_e_value = 0;
        gcode.current_extruder = 0;
        gcode.current_fan_speed = -1;
        gcode.total_print_times = std::vector<Duration>(static_cast<unsigned char>(PrintFeatureType::NumPrintFeatureTypes), 0.0);
        gcode.currentSpeed = 1;
        gcode.current_print_acceleration = -1;
        gcode.current_travel_acceleration = -1;
        gcode.current_jerk = -1;
        gcode.is_z_hopped = 0;
        gcode.setFlavor(EGCodeFlavor::MARLIN);
        gcode.initial_bed_temp = 0;
        gcode.bed_temperature = 0;
        gcode.fan_number = 0;
        gcode.total_bounding_box = AABB3D();

        gcode.new_line = "\n"; //Not BFB flavour by default.
        gcode.machine_name = "Your favourite 3D printer";
        gcode.machine_buildplate_type = "Your favourite build plate";

        //Set up a scene so that we may request settings.
        Application::getInstance().current_slice = new Slice(0);
    }

    void TearDown()
    {
        delete Application::getInstance().current_slice;
    }
};

TEST_P(GriffinHeaderTest, HeaderGriffinFormat)
{
    const size_t num_extruders = GetParam();
    gcode.flavor = EGCodeFlavor::GRIFFIN;
    for (size_t extruder_index = 0; extruder_index < num_extruders; extruder_index++)
    {
        Application::getInstance().current_slice->scene.extruders.emplace_back(extruder_index, nullptr);
        ExtruderTrain& train = Application::getInstance().current_slice->scene.extruders.back();
        train.settings.add("machine_nozzle_size", "0.4");
        train.settings.add("machine_nozzle_id", "TestNozzle");
    }

    const std::vector<bool> extruder_is_used(num_extruders, true);
    std::istringstream result(gcode.getFileHeader(extruder_is_used));
    std::string token;

    std::getline(result, token, '\n');
    EXPECT_EQ(std::string(";START_OF_HEADER"), token);
    std::getline(result, token, '\n');
    EXPECT_EQ(std::string(";HEADER_VERSION:"), token.substr(0, 16)); //Actual version doesn't matter in this test.
    std::getline(result, token, '\n');
    EXPECT_EQ(std::string(";FLAVOR:Griffin"), token);
    std::getline(result, token, '\n');
    EXPECT_EQ(std::string(";GENERATOR.NAME:Cura_SteamEngine"), token);
    std::getline(result, token, '\n');
    EXPECT_EQ(std::string(";GENERATOR.VERSION:"), token.substr(0, 19));
    // debian/rules sets this to the actual package version instead of "master":
    EXPECT_EQ(std::getenv("DEB_VERSION_UPSTREAM"), token.substr(19));
    std::getline(result, token, '\n');
    EXPECT_EQ(std::string(";GENERATOR.BUILD_DATE:"), token.substr(0, 22));
    EXPECT_EQ(Date::getDate().toStringDashed(), token.substr(22));
    std::getline(result, token, '\n');
    EXPECT_EQ(std::string(";TARGET_MACHINE.NAME:"), token.substr(0, 21));
    EXPECT_EQ(gcode.machine_name, token.substr(21));

    for (size_t extruder_nr = 0; extruder_nr < num_extruders; extruder_nr++)
    {
        std::getline(result, token, '\n');
        EXPECT_EQ(std::string(";EXTRUDER_TRAIN."), token.substr(0, 16));
        EXPECT_EQ(std::to_string(extruder_nr), token.substr(16, 1)); //TODO: Assumes the extruder nr is 1 digit.
        EXPECT_EQ(std::string(".INITIAL_TEMPERATURE:"), token.substr(17, 21)); //Actual temperature doesn't matter.
        std::getline(result, token, '\n');
        EXPECT_EQ(std::string(";EXTRUDER_TRAIN."), token.substr(0, 16));
        EXPECT_EQ(std::to_string(extruder_nr), token.substr(16, 1)); //TODO: Assumes the extruder nr is 1 digit.
        EXPECT_EQ(std::string(".NOZZLE.DIAMETER:0.4"), token.substr(17, 20)); //Nozzle size needs to be equal to the machine_nozzle_size setting.
        std::getline(result, token, '\n');
        EXPECT_EQ(std::string(";EXTRUDER_TRAIN."), token.substr(0, 16));
        EXPECT_EQ(std::to_string(extruder_nr), token.substr(16, 1)); //TODO: Assumes the extruder nr is 1 digit.
        EXPECT_EQ(std::string(".NOZZLE.NAME:TestNozzle"), token.substr(17, 23)); //Nozzle name needs to be equal to the machine_nozzle_id setting.
    }

    std::getline(result, token, '\n');
    EXPECT_EQ(std::string(";BUILD_PLATE.TYPE:"), token.substr(0, 18));
    EXPECT_EQ(gcode.machine_buildplate_type, token.substr(18));
    std::getline(result, token, '\n');
    EXPECT_EQ(std::string(";BUILD_PLATE.INITIAL_TEMPERATURE:"), token.substr(0, 33)); //Actual temperature doesn't matter in this test.
    std::getline(result, token, '\n');
    EXPECT_EQ(std::string(";PRINT.GROUPS:0"), token);
    std::getline(result, token, '\n');
    EXPECT_EQ(std::string(";PRINT.SIZE.MIN.X:"), token.substr(0, 18)); //Actual bounds don't matter in this test.
    std::getline(result, token, '\n');
    EXPECT_EQ(std::string(";PRINT.SIZE.MIN.Y:"), token.substr(0, 18));
    std::getline(result, token, '\n');
    EXPECT_EQ(std::string(";PRINT.SIZE.MIN.Z:"), token.substr(0, 18));
    std::getline(result, token, '\n');
    EXPECT_EQ(std::string(";PRINT.SIZE.MAX.X:"), token.substr(0, 18));
    std::getline(result, token, '\n');
    EXPECT_EQ(std::string(";PRINT.SIZE.MAX.Y:"), token.substr(0, 18));
    std::getline(result, token, '\n');
    EXPECT_EQ(std::string(";PRINT.SIZE.MAX.Z:"), token.substr(0, 18));
    std::getline(result, token, '\n');
    EXPECT_EQ(std::string(";END_OF_HEADER"), token);
}

INSTANTIATE_TEST_CASE_P(GriffinHeaderTestInstantiation, GriffinHeaderTest, testing::Values(0, 1, 2, 9));

/*
 * Test the default header generation.
 */
TEST_F(GCodeExportTest, HeaderUltiGCode)
{
    gcode.flavor = EGCodeFlavor::ULTIGCODE;
    constexpr size_t num_extruders = 2;
    const std::vector<bool> extruder_is_used(num_extruders, true);
    constexpr Duration print_time = 1337;
    const std::vector<double> filament_used = {100, 200};
    for (size_t extruder_index = 0; extruder_index < num_extruders; extruder_index++)
    {
        Application::getInstance().current_slice->scene.extruders.emplace_back(extruder_index, nullptr);
        ExtruderTrain& train = Application::getInstance().current_slice->scene.extruders.back();
        train.settings.add("machine_nozzle_size", "0.4");
    }
    gcode.total_bounding_box = AABB3D(Point3(0, 0, 0), Point3(1000, 1000, 1000));

    std::string result = gcode.getFileHeader(extruder_is_used, &print_time, filament_used);

    EXPECT_EQ(result, ";FLAVOR:UltiGCode\n;TIME:1337\n;MATERIAL:100\n;MATERIAL2:200\n;NOZZLE_DIAMETER:0.4\n;MINX:0\n;MINY:0\n;MINZ:0\n;MAXX:1\n;MAXY:1\n;MAXZ:1\n");
}

TEST_F(GCodeExportTest, HeaderRepRap)
{
    Application::getInstance().current_slice->scene.current_mesh_group->settings.add("layer_height", "0.123");
    gcode.flavor = EGCodeFlavor::REPRAP;
    gcode.extruder_attr[0].filament_area = 5.0;
    gcode.extruder_attr[1].filament_area = 4.0;
    constexpr size_t num_extruders = 2;
    const std::vector<bool> extruder_is_used(num_extruders, true);
    constexpr Duration print_time = 1337;
    const std::vector<double> filament_used = {100, 200};
    gcode.total_bounding_box = AABB3D(Point3(0, 0, 0), Point3(1000, 1000, 1000));

    std::string result = gcode.getFileHeader(extruder_is_used, &print_time, filament_used);

    EXPECT_EQ(result, ";FLAVOR:RepRap\n;TIME:1337\n;Filament used: 0.02m, 0.05m\n;Layer height: 0.123\n;MINX:0\n;MINY:0\n;MINZ:0\n;MAXX:1\n;MAXY:1\n;MAXZ:1\n");
}

TEST_F(GCodeExportTest, HeaderMarlin)
{
    Application::getInstance().current_slice->scene.current_mesh_group->settings.add("layer_height", "0.123");
    gcode.flavor = EGCodeFlavor::MARLIN;
    gcode.extruder_attr[0].filament_area = 5.0;
    gcode.extruder_attr[1].filament_area = 4.0;
    constexpr size_t num_extruders = 2;
    const std::vector<bool> extruder_is_used(num_extruders, true);
    constexpr Duration print_time = 1337;
    const std::vector<double> filament_used = {100, 200};
    gcode.total_bounding_box = AABB3D(Point3(0, 0, 0), Point3(1000, 1000, 1000));

    std::string result = gcode.getFileHeader(extruder_is_used, &print_time, filament_used);

    EXPECT_EQ(result, ";FLAVOR:Marlin\n;TIME:1337\n;Filament used: 0.02m, 0.05m\n;Layer height: 0.123\n;MINX:0\n;MINY:0\n;MINZ:0\n;MAXX:1\n;MAXY:1\n;MAXZ:1\n");
}

TEST_F(GCodeExportTest, HeaderMarlinVolumetric)
{
    Application::getInstance().current_slice->scene.current_mesh_group->settings.add("layer_height", "0.123");
    gcode.flavor = EGCodeFlavor::MARLIN_VOLUMATRIC;
    constexpr size_t num_extruders = 2;
    const std::vector<bool> extruder_is_used(num_extruders, true);
    constexpr Duration print_time = 1337;
    const std::vector<double> filament_used = {100, 200};
    gcode.total_bounding_box = AABB3D(Point3(0, 0, 0), Point3(1000, 1000, 1000));

    std::string result = gcode.getFileHeader(extruder_is_used, &print_time, filament_used);

    EXPECT_EQ(result, ";FLAVOR:Marlin(Volumetric)\n;TIME:1337\n;Filament used: 100mm3, 200mm3\n;Layer height: 0.123\n;MINX:0\n;MINY:0\n;MINZ:0\n;MAXX:1\n;MAXY:1\n;MAXZ:1\n");
}

/*
 * Test conversion from E values to millimetres and back in the case of a
 * volumetric printer.
 */
TEST_F(GCodeExportTest, EVsMmVolumetric)
{
    constexpr double filament_area = 10.0;
    gcode.extruder_attr[0].filament_area = filament_area;
    gcode.is_volumetric = true;

    constexpr double mm3_input = 15.0;
    EXPECT_EQ(gcode.mm3ToE(mm3_input), mm3_input) << "Since the E is volumetric and the input mm3 is also volumetric, the output needs to be the same.";

    EXPECT_EQ(gcode.eToMm(200.0), 200.0 / filament_area) << "Since the E is volumetric but mm is linear, divide by the cross-sectional area of the filament to convert the volume to a length.";

    constexpr double mm_input = 33.0;
    EXPECT_EQ(gcode.mmToE(mm_input), mm_input * filament_area) << "Since the input mm is linear but the E output must be volumetric, we need to multiply by the cross-sectional area to convert length to volume.";

    constexpr double e_input = 100.0;
    EXPECT_EQ(gcode.eToMm3(e_input, 0), e_input) << "Since the E is volumetric and mm3 is also volumetric, the output needs to be the same.";
}

/*
 * Test conversion from E values to millimetres and back in the case where the E
 * value represents the linear position of the filament.
 */
TEST_F(GCodeExportTest, EVsMmLinear)
{
    constexpr double filament_area = 10.0;
    gcode.extruder_attr[0].filament_area = filament_area;
    gcode.is_volumetric = false;

    EXPECT_EQ(gcode.mmToE(15.0), 15.0) << "Since the E is linear and the input mm is also linear, the output needs to be the same.";
    EXPECT_EQ(gcode.eToMm(15.0), 15.0) << "Since the E is linear and the output mm is also linear, the output needs to be the same.";

    for(double x = -1000.0; x < 1000.0; x += 16.0)
    {
        EXPECT_DOUBLE_EQ(gcode.mmToE(gcode.eToMm(x)), x) << "Converting back and forth should lead to the same number.";
    }

    constexpr double mm3_input = 33.0;
    EXPECT_EQ(gcode.mm3ToE(mm3_input), mm3_input / filament_area) << "Since the input mm3 is volumetric but the E output must be linear, we need to divide by the cross-sectional area to convert volume to length.";

    constexpr double e_input = 100.0;
    EXPECT_EQ(gcode.eToMm3(e_input, 0), e_input * filament_area) << "Since the input E is linear but the output must be volumetric, we need to multiply by cross-sectional area to convert length to volume.";
}

/*
 * Switch extruders, with the following special cases:
 * - No retraction distance.
 */
TEST_F(GCodeExportTest, SwitchExtruderSimple)
{
    Scene& scene = Application::getInstance().current_slice->scene;

    scene.extruders.emplace_back(0, nullptr);
    ExtruderTrain& train1 = scene.extruders.back();
    train1.settings.add("machine_extruder_start_code", ";FIRST EXTRUDER START G-CODE!");
    train1.settings.add("machine_extruder_end_code", ";FIRST EXTRUDER END G-CODE!");
    train1.settings.add("machine_firmware_retract", "True");
    train1.settings.add("retraction_enable", "True");
    scene.extruders.emplace_back(1, nullptr);
    ExtruderTrain& train2 = scene.extruders.back();
    train2.settings.add("machine_extruder_start_code", ";SECOND EXTRUDER START G-CODE!");
    train2.settings.add("machine_extruder_end_code", ";SECOND EXTRUDER END G-CODE!");
    train2.settings.add("machine_firmware_retract", "True");
    train2.settings.add("retraction_enable", "True");

    RetractionConfig no_retraction;
    no_retraction.distance = 0;

    EXPECT_CALL(*mock_communication, setExtruderForSend(testing::_));
    EXPECT_CALL(*mock_communication, sendCurrentPosition(testing::_));
    gcode.switchExtruder(1, no_retraction);

    EXPECT_EQ(std::string("G92 E0\n;FIRST EXTRUDER END G-CODE!\nT1\nG92 E0\n;SECOND EXTRUDER START G-CODE!\n"), output.str());
}

TEST_F(GCodeExportTest, WriteZHopStartZero)
{
    gcode.writeZhopStart(0);
    EXPECT_EQ(std::string(""), output.str()) << "Zero length z hop shouldn't affect gcode output.";
}

TEST_F(GCodeExportTest, WriteZHopStartDefaultSpeed)
{
    Application::getInstance().current_slice->scene.extruders.emplace_back(0, nullptr);
    Application::getInstance().current_slice->scene.extruders[gcode.current_extruder].settings.add("speed_z_hop", "1"); //60mm/min.
    gcode.current_layer_z = 2000;
    constexpr coord_t hop_height = 3000;
    gcode.writeZhopStart(hop_height);
    EXPECT_EQ(std::string("G1 F60 Z5\n"), output.str());
}

TEST_F(GCodeExportTest, WriteZHopStartCustomSpeed)
{
    Application::getInstance().current_slice->scene.extruders.emplace_back(0, nullptr);
    Application::getInstance().current_slice->scene.extruders[gcode.current_extruder].settings.add("speed_z_hop", "1"); //60mm/min.
    gcode.current_layer_z = 2000;
    constexpr coord_t hop_height = 3000;
    constexpr Velocity speed = 4; // 240 mm/min.
    gcode.writeZhopStart(hop_height, speed);
    EXPECT_EQ(std::string("G1 F240 Z5\n"), output.str()) << "Custom provided speed should be used.";
}

TEST_F(GCodeExportTest, WriteZHopEndZero)
{
    gcode.is_z_hopped = 0;
    gcode.writeZhopEnd();
    EXPECT_EQ(std::string(""), output.str()) << "Zero length z hop shouldn't affect gcode output.";
}

TEST_F(GCodeExportTest, WriteZHopEndDefaultSpeed)
{
    Application::getInstance().current_slice->scene.extruders.emplace_back(0, nullptr);
    Application::getInstance().current_slice->scene.extruders[gcode.current_extruder].settings.add("speed_z_hop", "1"); //60mm/min.
    gcode.current_layer_z = 2000;
    gcode.is_z_hopped = 3000;
    gcode.writeZhopEnd();
    EXPECT_EQ(std::string("G1 F60 Z2\n"), output.str());
}

TEST_F(GCodeExportTest, WriteZHopEndCustomSpeed)
{
    Application::getInstance().current_slice->scene.extruders.emplace_back(0, nullptr);
    Application::getInstance().current_slice->scene.extruders[gcode.current_extruder].settings.add("speed_z_hop", "1");
    gcode.current_layer_z = 2000;
    gcode.is_z_hopped = 3000;
    constexpr Velocity speed = 4; // 240 mm/min.
    gcode.writeZhopEnd(speed);
    EXPECT_EQ(std::string("G1 F240 Z2\n"), output.str()) << "Custom provided speed should be used.";
}

TEST_F(GCodeExportTest, insertWipeScriptSingleMove)
{
    gcode.currentPosition = Point3(1000, 1000, 1000);
    gcode.current_layer_z = 1000;
    gcode.use_extruder_offset_to_offset_coords = false;
    Application::getInstance().current_slice->scene.current_mesh_group->settings.add("layer_height", "0.2");

    WipeScriptConfig config;
    config.retraction_enable = false;
    config.hop_enable = false;
    config.brush_pos_x = 2000;
    config.repeat_count = 1;
    config.move_distance = 500;
    config.move_speed = 10;
    config.pause = 0;

    EXPECT_CALL(*mock_communication, sendLineTo(testing::_, testing::_, testing::_, testing::_, testing::_)).Times(3);
    gcode.insertWipeScript(config);

    std::string token;
    std::getline(output, token, '\n');
    EXPECT_EQ(std::string(";WIPE_SCRIPT_BEGIN"), token) << "Wipe script should always start with tag.";
    std::getline(output, token, '\n');
    EXPECT_EQ(std::string("G0 F600 X2 Y1"), token) << "Wipe script should go to its position.";
    std::getline(output, token, '\n');
    EXPECT_EQ(std::string("G0 X2.5 Y1"), token) << "There should be one wipe move.";
    std::getline(output, token, '\n');
    EXPECT_EQ(std::string("G0 X1 Y1"), token) << "Wipe script should return back to position before wipe.";
    std::getline(output, token, '\n');
    EXPECT_EQ(std::string(";WIPE_SCRIPT_END"), token) << "Wipe script should always end with tag.";
}

TEST_F(GCodeExportTest, insertWipeScriptMultipleMoves)
{
    gcode.currentPosition = Point3(1000, 1000, 1000);
    gcode.current_layer_z = 1000;
    gcode.use_extruder_offset_to_offset_coords = false;
    Application::getInstance().current_slice->scene.current_mesh_group->settings.add("layer_height", "0.2");

    WipeScriptConfig config;
    config.retraction_enable = false;
    config.hop_enable = false;
    config.brush_pos_x = 2000;
    config.repeat_count = 4;
    config.move_distance = 500;
    config.move_speed = 10;
    config.pause = 0;

    EXPECT_CALL(*mock_communication, sendLineTo(testing::_, testing::_, testing::_, testing::_, testing::_)).Times(6);
    gcode.insertWipeScript(config);

    std::string token;
    std::getline(output, token, '\n');
    EXPECT_EQ(std::string(";WIPE_SCRIPT_BEGIN"), token) << "Wipe script should always start with tag.";
    std::getline(output, token, '\n');
    EXPECT_EQ(std::string("G0 F600 X2 Y1"), token) << "Wipe script should go to its position.";
    std::getline(output, token, '\n');
    EXPECT_EQ(std::string("G0 X2.5 Y1"), token);
    std::getline(output, token, '\n');
    EXPECT_EQ(std::string("G0 X2 Y1"), token);
    std::getline(output, token, '\n');
    EXPECT_EQ(std::string("G0 X2.5 Y1"), token);
    std::getline(output, token, '\n');
    EXPECT_EQ(std::string("G0 X2 Y1"), token);
    std::getline(output, token, '\n');
    EXPECT_EQ(std::string("G0 X1 Y1"), token) << "Wipe script should return back to position before wipe.";
    std::getline(output, token, '\n');
    EXPECT_EQ(std::string(";WIPE_SCRIPT_END"), token) << "Wipe script should always end with tag.";
}

TEST_F(GCodeExportTest, insertWipeScriptOptionalDelay)
{
    gcode.currentPosition = Point3(1000, 1000, 1000);
    gcode.current_layer_z = 1000;
    gcode.use_extruder_offset_to_offset_coords = false;
    Application::getInstance().current_slice->scene.current_mesh_group->settings.add("layer_height", "0.2");

    WipeScriptConfig config;
    config.retraction_enable = false;
    config.hop_enable = false;
    config.brush_pos_x = 2000;
    config.repeat_count = 1;
    config.move_distance = 500;
    config.move_speed = 10;
    config.pause = 1.5; // 1.5 sec = 1500 ms.

    EXPECT_CALL(*mock_communication, sendLineTo(testing::_, testing::_, testing::_, testing::_, testing::_)).Times(3);
    gcode.insertWipeScript(config);

    std::string token;
    std::getline(output, token, '\n');
    EXPECT_EQ(std::string(";WIPE_SCRIPT_BEGIN"), token) << "Wipe script should always start with tag.";
    std::getline(output, token, '\n'); // go to wipe position
    std::getline(output, token, '\n'); // make wipe move
    std::getline(output, token, '\n'); // return back
    std::getline(output, token, '\n');
    EXPECT_EQ(std::string("G4 P1500"), token) << "Wipe script should make a delay.";
    std::getline(output, token, '\n');
    EXPECT_EQ(std::string(";WIPE_SCRIPT_END"), token) << "Wipe script should always end with tag.";
}

TEST_F(GCodeExportTest, insertWipeScriptRetractionEnable)
{
    gcode.currentPosition = Point3(1000, 1000, 1000);
    gcode.current_layer_z = 1000;
    gcode.current_e_value = 100;
    gcode.use_extruder_offset_to_offset_coords = false;
    gcode.is_volumetric = false;
    gcode.current_extruder = 0;
    gcode.extruder_attr[0].filament_area = 10.0;
    gcode.relative_extrusion = false;
    gcode.currentSpeed = 1;
    Application::getInstance().current_slice->scene.current_mesh_group->settings.add("layer_height", "0.2");
    Application::getInstance().current_slice->scene.extruders.emplace_back(0, &Application::getInstance().current_slice->scene.current_mesh_group->settings);
    Application::getInstance().current_slice->scene.extruders.back().settings.add("machine_firmware_retract", "false");

    WipeScriptConfig config;
    config.retraction_enable = true;
    config.retraction_config.distance = 1;
    config.retraction_config.speed = 2; // 120 mm/min.
    config.retraction_config.primeSpeed = 3; // 180 mm/min.
    config.retraction_config.prime_volume = gcode.extruder_attr[0].filament_area * 4; // 4mm in linear dimensions
    config.retraction_config.retraction_count_max = 100; //Practically no limit.
    config.retraction_config.retraction_extrusion_window = 1;
    config.retraction_config.retraction_min_travel_distance = 0; //Don't limit retractions for being too short.
    config.hop_enable = false;
    config.brush_pos_x = 2000;
    config.repeat_count = 1;
    config.move_distance = 500;
    config.move_speed = 10;
    config.pause = 0;

    EXPECT_CALL(*mock_communication, sendLineTo(testing::_, testing::_, testing::_, testing::_, testing::_)).Times(3);
    gcode.insertWipeScript(config);

    std::string token;
    std::getline(output, token, '\n');
    EXPECT_EQ(std::string(";WIPE_SCRIPT_BEGIN"), token) << "Wipe script should always start with tag.";
    std::getline(output, token, '\n');
    EXPECT_EQ(std::string("G1 F120 E99"), token) << "Wipe script should perform retraction with provided speed and retraction distance.";
    std::getline(output, token, '\n'); // go to wipe position
    std::getline(output, token, '\n'); // make wipe move
    std::getline(output, token, '\n'); // return back
    std::getline(output, token, '\n');
    EXPECT_EQ(std::string("G1 F180 E104"), token) << "Wipe script should make unretraction with provided speed and extra prime volume.";
    std::getline(output, token, '\n');
    EXPECT_EQ(std::string(";WIPE_SCRIPT_END"), token) << "Wipe script should always end with tag.";
}

TEST_F(GCodeExportTest, insertWipeScriptHopEnable)
{
    gcode.currentPosition = Point3(1000, 1000, 1000);
    gcode.current_layer_z = 1000;
    gcode.use_extruder_offset_to_offset_coords = false;
    gcode.currentSpeed = 1;
    Application::getInstance().current_slice->scene.current_mesh_group->settings.add("layer_height", "0.2");

    WipeScriptConfig config;
    config.retraction_enable = false;
    config.hop_enable = true;
    config.hop_speed = 2; // 120 mm/min.
    config.hop_amount = 300;
    config.brush_pos_x = 2000;
    config.repeat_count = 1;
    config.move_distance = 500;
    config.move_speed = 10;
    config.pause = 0;

    EXPECT_CALL(*mock_communication, sendLineTo(testing::_, testing::_, testing::_, testing::_, testing::_)).Times(3);
    gcode.insertWipeScript(config);

    std::string token;
    std::getline(output, token, '\n');
    EXPECT_EQ(std::string(";WIPE_SCRIPT_BEGIN"), token) << "Wipe script should always start with tag.";
    std::getline(output, token, '\n');
    EXPECT_EQ(std::string("G1 F120 Z1.3"), token) << "Wipe script should perform z-hop.";
    std::getline(output, token, '\n'); // go to wipe position
    std::getline(output, token, '\n'); // make wipe move
    std::getline(output, token, '\n'); // return back
    std::getline(output, token, '\n');
    EXPECT_EQ(std::string("G1 F120 Z1"), token) << "Wipe script should return z position.";
    std::getline(output, token, '\n');
    EXPECT_EQ(std::string(";WIPE_SCRIPT_END"), token) << "Wipe script should always end with tag.";
}

} //namespace cura