예제 #1
0
        public void TestDisplayQuestionMenu()
        {
            var menu = new QuestionMenu(
                "Which option would you choose?",
                new[]
            {
                "Option 1",
                "Option 2",
                "Option 3"
            }
                );

            var data = new DisplayQuestionMenu(menu).Compile();

            var question       = menu.Question;
            var questionLength = Math.Min(255, question.Length);
            var answersCount   = 0;
            var length         = 11 + questionLength;

            foreach (var answer in menu.Answers)
            {
                length += 5 + answer.Length;
                if (answersCount == 255)
                {
                    break;
                }

                answersCount++;
            }

            Span <byte> expectedData = stackalloc byte[length];

            var pos = 0;

            expectedData.Write(ref pos, (byte)0x7C); // Packet ID
            expectedData.Write(ref pos, (ushort)length);
            expectedData.Write(ref pos, menu.Serial);
            expectedData.Write(ref pos, (ushort)0x00);
            expectedData.Write(ref pos, (byte)question.Length);
            expectedData.WriteAscii(ref pos, question, 255);
            expectedData.Write(ref pos, (byte)answersCount);
            for (var i = 0; i < answersCount; i++)
            {
                var answer = menu.Answers[i];
#if NO_LOCAL_INIT
                expectedData.Write(ref pos, 0);
#else
                pos += 4;
#endif
                expectedData.Write(ref pos, (byte)Math.Min(255, answer.Length));
                expectedData.WriteAscii(ref pos, answer, 255);
            }

            AssertThat.Equal(data, expectedData);
        }
예제 #2
0
        public void TestDisplayQuestionMenu()
        {
            var menu = new QuestionMenu(
                "Which option would you choose?",
                new[]
            {
                "Option 1",
                "Option 2",
                "Option 3"
            }
                );

            string question       = menu.Question?.Trim() ?? "";
            int    questionLength = Math.Min(255, question.Length);

            int length = 11 + questionLength + menu.Answers.Sum(answer => 5 + answer?.Trim().Length ?? 0);

            Span <byte> data = new DisplayQuestionMenu(menu).Compile();

            Span <byte> expectedData = stackalloc byte[length];

            int pos = 0;

            expectedData[pos++] = 0x7C; // Packet ID
            ((ushort)length).CopyTo(ref pos, expectedData);
            menu.Serial.CopyTo(ref pos, expectedData);
            pos += 2; // ((ushort)0x00).CopyTo(ref pos, expectedData);
            expectedData[pos++] = (byte)questionLength;
            question?.CopyASCIITo(ref pos, expectedData);
            expectedData[pos++] = (byte)menu.Answers.Length;
            for (int i = 0; i < menu.Answers.Length; i++)
            {
                pos += 4; // 0x0.CopyTo(ref pos, expectedData);
                var answer = menu.Answers[i]?.Trim() ?? "";
                expectedData[pos++] = (byte)answer.Length;
                answer.CopyASCIITo(ref pos, expectedData);
            }

            AssertThat.Equal(data, expectedData);
        }