Пример #1
0
    // End game, stop animations, sounds, physics. Finish recording metrics
    void EndGame()
    {
        mcMetric.finishRecording();
        var str = metricWriter.GetLogMetrics(
            DateTime.Now,
            new List <AbstractMetric>()
        {
            mcMetric
        }
            );

        StartCoroutine(Post("feeder_" + DateTime.Now.ToFileTime() + ".json", str));

        dispenser.StopAllCoroutines();
        dispenser.screenRed.SetActive(false);
        dispenser.screenGreen.SetActive(false);
        dispenser.enabled = false;
        monster.speed     = 0f;
        foreach (GameObject obj in GameObject.FindGameObjectsWithTag("Food"))
        {
            obj.GetComponent <Rigidbody2D>().isKinematic     = true;
            obj.GetComponent <Rigidbody2D>().velocity        = Vector2.zero;
            obj.GetComponent <Rigidbody2D>().angularVelocity = 0f;
        }
        foreach (AudioSource aud in FindObjectsOfType(typeof(AudioSource)) as AudioSource[])
        {
            aud.Stop();
        }
        EndLevel(0f);
    }
    public void TestStartFinishRecording()
    {
        MemoryChoiceMetric mcm = new MemoryChoiceMetric();

        Assert.IsFalse(mcm.isRecording);    // isRecording start out false

        mcm.startRecording();
        Assert.IsTrue(mcm.isRecording);     // Should be true now

        mcm.finishRecording();
        Assert.IsFalse(mcm.isRecording);    // Should be false now

        mcm.startRecording();
        Assert.IsTrue(mcm.isRecording);
        mcm.startRecording();
        Assert.IsTrue(mcm.isRecording);     // Redundant calls to startRecording still keep it true

        mcm.finishRecording();
        Assert.IsFalse(mcm.isRecording);
        mcm.finishRecording();
        Assert.IsFalse(mcm.isRecording);    // Redundant calls to finishRecording still keep is false
    }
    public void TestGetJSON()
    {
        // Test getJSON() when metric has no records
        MemoryChoiceMetric mcm1 = new MemoryChoiceMetric();

        Assert.IsEmpty(mcm1.eventList);

        JObject json1 = mcm1.getJSON();

        Assert.IsNotNull(json1);
        Assert.IsNotNull(json1["metricName"]);      // JSON entry "memoryChoice" should be empty, as nothing has been recorded
        Assert.IsEmpty((JArray)json1["eventList"]);

        // Test getJSON() with one record
        MemoryChoiceMetric mcm2 = new MemoryChoiceMetric();

        mcm2.startRecording();
        Assert.IsTrue(mcm2.isRecording);
        Assert.IsEmpty(mcm2.eventList);

        mcm2.recordEvent(new MemoryChoiceEvent(new System.DateTime(2021, 2, 1), new List <string>(new string[] { "apple" }), "pear", false, new System.DateTime(2021, 2, 1, 1, 2, 3)));
        mcm2.finishRecording();

        JObject json2 = mcm2.getJSON();

        Assert.IsNotNull(json2);
        Assert.IsNotNull(json2["metricName"]);

        JArray eventList2 = (JArray)json2["eventList"];

        Assert.AreEqual(1, eventList2.Count);
        Assert.AreEqual("2021-02-01 12:00:00 AM", eventList2[0]["eventTime"].ToString());
        Debug.Log(json2);
        JArray json2mc0os = (JArray)eventList2[0]["objectsSet"];

        Assert.IsNotEmpty(json2mc0os);
        Assert.AreEqual(1, json2mc0os.Count);
        Assert.AreEqual("apple", json2mc0os[0].ToString());
        Assert.AreEqual("pear", eventList2[0]["_object"].ToString());
        Assert.AreEqual(false, eventList2[0]["choice"].ToObject <bool>());
        Assert.AreEqual("2021-02-01 1:02:03 AM", eventList2[0]["choiceTime"].ToString());


        // Test getJSON() with multiple records
        MemoryChoiceMetric mcm3 = new MemoryChoiceMetric();

        mcm3.startRecording();
        Assert.IsTrue(mcm3.isRecording);
        Assert.IsEmpty(mcm3.eventList);

        mcm3.recordEvent(new MemoryChoiceEvent(new System.DateTime(2021, 2, 1), new List <string>(new string[] { "apple", "pear" }), "banana", false, new System.DateTime(2021, 2, 1, 1, 2, 3)));
        mcm3.recordEvent(new MemoryChoiceEvent(new System.DateTime(2021, 2, 2), new List <string>(new string[] { "apple" }), "pear", false, new System.DateTime(2021, 2, 2, 1, 2, 3)));
        mcm3.recordEvent(new MemoryChoiceEvent(new System.DateTime(2021, 2, 3), new List <string>(new string[] { "orange" }), "grapes", true, new System.DateTime(2021, 2, 3, 1, 2, 3)));
        mcm3.finishRecording();
        mcm3.recordEvent(new MemoryChoiceEvent(new System.DateTime(2021, 2, 4), new List <string>(new string[] { "apple" }), "pear", false, new System.DateTime(2021, 2, 4, 1, 2, 3)));    // record an event after finishing recording
        Assert.IsFalse(mcm3.isRecording);

        JObject json3 = mcm3.getJSON();

        Assert.IsNotNull(json3);
        Assert.IsNotNull(json3["metricName"]);

        JArray eventList3 = (JArray)json3["eventList"];

        Assert.AreEqual(3, eventList3.Count);

        Assert.AreEqual("2021-02-01 12:00:00 AM", eventList3[0]["eventTime"].ToString());
        JArray json3mc0os = (JArray)eventList3[0]["objectsSet"];

        Assert.IsNotEmpty(json3mc0os);
        Assert.AreEqual(2, json3mc0os.Count);
        Assert.AreEqual("apple", json3mc0os[0].ToString());
        Assert.AreEqual("pear", json3mc0os[1].ToString());
        Assert.AreEqual("banana", eventList3[0]["_object"].ToString());
        Assert.AreEqual(false, eventList3[0]["choice"].ToObject <bool>());
        Assert.AreEqual("2021-02-01 1:02:03 AM", eventList3[0]["choiceTime"].ToString());

        Assert.AreEqual("2021-02-02 12:00:00 AM", eventList3[1]["eventTime"].ToString());
        JArray json3mc1os = (JArray)eventList3[1]["objectsSet"];

        Assert.IsNotEmpty(json3mc1os);
        Assert.AreEqual(1, json3mc1os.Count);
        Assert.AreEqual("apple", json3mc1os[0].ToString());
        Assert.AreEqual("pear", eventList3[1]["_object"].ToString());
        Assert.AreEqual(false, eventList3[1]["choice"].ToObject <bool>());
        Assert.AreEqual("2021-02-02 1:02:03 AM", eventList3[1]["choiceTime"].ToString());

        Assert.AreEqual("2021-02-03 12:00:00 AM", eventList3[2]["eventTime"].ToString());
        JArray json3mc2os = (JArray)eventList3[2]["objectsSet"];

        Assert.IsNotEmpty(json3mc2os);
        Assert.AreEqual(1, json3mc2os.Count);
        Assert.AreEqual("orange", json3mc2os[0].ToString());
        Assert.AreEqual("grapes", eventList3[2]["_object"].ToString());
        Assert.AreEqual(true, eventList3[2]["choice"].ToObject <bool>());
        Assert.AreEqual("2021-02-03 1:02:03 AM", eventList3[2]["choiceTime"].ToString());
    }
    public void TestRecordEvent()
    {
        // Test adding null event (state of bpm1 shouldn't change)
        MemoryChoiceMetric mcm1 = new MemoryChoiceMetric();

        mcm1.startRecording();
        Assert.IsEmpty(mcm1.eventList);
        Assert.IsTrue(mcm1.isRecording);

        mcm1.recordEvent(null);
        Assert.IsEmpty(mcm1.eventList);

        // Test adding events to a MemoryChoiceEvent which has not started recording
        MemoryChoiceMetric mcm2 = new MemoryChoiceMetric();

        Assert.IsFalse(mcm2.isRecording);
        Assert.IsEmpty(mcm2.eventList);

        mcm2.recordEvent(new MemoryChoiceEvent(new System.DateTime(2021, 2, 1), new List <string>(new string[] { "apple" }), "apple", true, new System.DateTime(2021, 2, 2)));
        Assert.IsEmpty(mcm2.eventList);

        // Test adding valid events to a MemoryChoiceMetric which has started recording
        MemoryChoiceMetric mcm3 = new MemoryChoiceMetric();

        mcm3.startRecording();
        Assert.IsTrue(mcm3.isRecording);
        Assert.IsEmpty(mcm3.eventList);

        mcm3.recordEvent(new MemoryChoiceEvent(new System.DateTime(2021, 2, 1), new List <string>(new string[] { "apple", "orange" }), "banana", true, new System.DateTime(2021, 2, 1)));
        mcm3.recordEvent(new MemoryChoiceEvent(new System.DateTime(2021, 2, 2), new List <string>(new string[] { "apple", "orange" }), "apple", true, new System.DateTime(2021, 2, 2)));
        mcm3.recordEvent(new MemoryChoiceEvent(new System.DateTime(2021, 2, 3), new List <string>(new string[] { "apple", "orange", "pear" }), "banana", false, new System.DateTime(2021, 2, 3)));
        mcm3.finishRecording();

        Assert.AreEqual(3, mcm3.eventList.Count);
        Assert.AreEqual(new System.DateTime(2021, 2, 1), mcm3.eventList[0].eventTime);
        Assert.IsNotEmpty(mcm3.eventList[0].objectsSet);
        Assert.AreEqual(2, mcm3.eventList[0].objectsSet.Count);
        Assert.AreEqual("apple", mcm3.eventList[0].objectsSet[0]);
        Assert.AreEqual("orange", mcm3.eventList[0].objectsSet[1]);
        Assert.AreEqual("banana", mcm3.eventList[0]._object);
        Assert.IsTrue(mcm3.eventList[0].choice);
        Assert.AreEqual(new System.DateTime(2021, 2, 1), mcm3.eventList[0].choiceTime);

        Assert.AreEqual(new System.DateTime(2021, 2, 2), mcm3.eventList[1].eventTime);
        Assert.IsNotEmpty(mcm3.eventList[1].objectsSet);
        Assert.AreEqual(2, mcm3.eventList[1].objectsSet.Count);
        Assert.AreEqual("apple", mcm3.eventList[1].objectsSet[0]);
        Assert.AreEqual("orange", mcm3.eventList[1].objectsSet[1]);
        Assert.AreEqual("apple", mcm3.eventList[1]._object);
        Assert.IsTrue(mcm3.eventList[1].choice);
        Assert.AreEqual(new System.DateTime(2021, 2, 2), mcm3.eventList[1].choiceTime);

        Assert.AreEqual(new System.DateTime(2021, 2, 3), mcm3.eventList[2].eventTime);
        Assert.IsNotEmpty(mcm3.eventList[2].objectsSet);
        Assert.AreEqual(3, mcm3.eventList[2].objectsSet.Count);
        Assert.AreEqual("apple", mcm3.eventList[2].objectsSet[0]);
        Assert.AreEqual("orange", mcm3.eventList[2].objectsSet[1]);
        Assert.AreEqual("pear", mcm3.eventList[2].objectsSet[2]);
        Assert.AreEqual("banana", mcm3.eventList[2]._object);
        Assert.IsFalse(mcm3.eventList[2].choice);
        Assert.AreEqual(new System.DateTime(2021, 2, 3), mcm3.eventList[2].choiceTime);
    }