public void TestOpenFitActivityCsvFile()
        {
            var file    = FitParserHelpers.ReadFitCsvFile(@"TestData\Activity.csv");
            var records = file.Records;

            Assert.AreEqual(32, records.Count);
            var record1 = file.Records[0];

            Assert.AreEqual("Definition", record1.Type);
            Assert.AreEqual(0, record1.LocalNumber);
            Assert.AreEqual("file_id", record1.Message);
            var field1 = record1.Fields[0];

            Assert.AreEqual("serial_number", field1.Name);
            Assert.AreEqual("1", field1.Value);
            Assert.AreEqual(String.Empty, field1.Units);
            var record32 = file.Records[31];

            Assert.AreEqual("Data", record32.Type);
            Assert.AreEqual(5, record32.LocalNumber);
            Assert.AreEqual("activity", record32.Message);
            var field2 = record32.Fields[1];

            Assert.AreEqual("total_timer_time", field2.Name);
            Assert.AreEqual("13.749", field2.Value);
            Assert.AreEqual("s", field2.Units);
        }
 public void TestPascalCaseToRubyCaseConverter()
 {
     Assert.AreEqual("foo", FitParserHelpers.ConvertPascalCaseToRubyCase("foo"));
     Assert.AreEqual("foo", FitParserHelpers.ConvertPascalCaseToRubyCase("Foo"));
     Assert.AreEqual("foo_bar", FitParserHelpers.ConvertPascalCaseToRubyCase("fooBar"));
     Assert.AreEqual("foo_bar", FitParserHelpers.ConvertPascalCaseToRubyCase("FooBar"));
     Assert.AreEqual("foo_b_b", FitParserHelpers.ConvertPascalCaseToRubyCase("FooBB"));
     Assert.AreEqual("b_b", FitParserHelpers.ConvertPascalCaseToRubyCase("BB"));
     Assert.AreEqual("b", FitParserHelpers.ConvertPascalCaseToRubyCase("B"));
     Assert.AreEqual("b", FitParserHelpers.ConvertPascalCaseToRubyCase("b"));
 }
        private string FormatFieldString(int recordNumber, FitCsvRecord csvRecord, List <Field> fitRecord)
        {
            var sb = new StringBuilder();

            sb.Append("\n\nRecord Offset in FIT file: " + recordNumber);
            sb.Append("\nCSV Record:\n");
            foreach (var csvField in csvRecord.Fields)
            {
                sb.Append(String.Format("{0}={1} {2}\n", csvField.Name, csvField.Value, csvField.Units));
            }
            sb.Append("\nFIT Record:\n");
            foreach (var fitField in fitRecord)
            {
                sb.Append(String.Format("{0}={1} {2}\n", FitParserHelpers.ConvertPascalCaseToRubyCase(fitField.Name), ConvertField(fitField.GetValue()), fitField.Units));
            }
            return(sb.ToString());
        }
        private void TestActivityFitFile(string filename, int expectedTotalMessages, int expectedTotalDefinitions)
        {
            // Read the answer CSV file
            var file = FitParserHelpers.ReadFitCsvFile(TESTDATA_PATH + filename + ".csv");

            // Decode a Fit file
            var fileStream = System.IO.File.OpenRead(TESTDATA_PATH + filename + ".fit");

            Assert.IsNotNull(fileStream);

            var decode = new Decode();

            Assert.IsNotNull(decode);

            var mesgBroadcaster = new MesgBroadcaster();

            Assert.IsNotNull(mesgBroadcaster);

            Assert.IsTrue(decode.IsFIT(fileStream));

            // Note that it is possible to have something that fails an integrity check, but we can still attempt to parse it
            Assert.IsTrue(decode.CheckIntegrity(fileStream));

            // Connect the Broadcaster to our event (message) source (in this case the Decoder)
            decode.MesgEvent           += mesgBroadcaster.OnMesg;
            decode.MesgDefinitionEvent += mesgBroadcaster.OnMesgDefinition;

            int totalMessages = 0, totalDefinitions = 0;
            int currentRecord = 0;

            mesgBroadcaster.MesgEvent += (sender, args) =>
            {
                foreach (var field in args.mesg.fields)
                {
                    string fieldName             = FitParserHelpers.ConvertPascalCaseToRubyCase(field.Name);
                    var    record                = file.Records[currentRecord];
                    string fieldValue            = ConvertField(field.GetValue()).ToString();
                    var    csvField              = record.GetField(fieldName);
                    string diagnosticFieldString = FormatFieldString(currentRecord, record, args.mesg.fields);
                    Assert.IsNotNull(csvField, diagnosticFieldString);
                    Assert.AreEqual(csvField.Value.ToString(), fieldValue, diagnosticFieldString);
                }
                totalMessages++;
                currentRecord++;
            };

            mesgBroadcaster.MesgDefinitionEvent += (sender, args) =>
            {
                foreach (var field in args.mesgDef.GetFields())
                {
                    var record = file.Records[currentRecord];
                }
                totalDefinitions++;
                currentRecord++;
            };

            Assert.IsTrue(decode.Read(fileStream));
            Assert.AreEqual(expectedTotalMessages, totalMessages);
            Assert.AreEqual(expectedTotalDefinitions, totalDefinitions);

            fileStream.Close();
        }