def testBatching_BatchCount(self):
     """Tests the GetBatchCount() debugging function."""
     count = -1
     with mayaUsdLib.DiagnosticBatchContext():
         with mayaUsdLib.DiagnosticBatchContext():
             count = mayaUsdLib.DiagnosticDelegate.GetBatchCount()
     self.assertEqual(count, 2)
     count = mayaUsdLib.DiagnosticDelegate.GetBatchCount()
     self.assertEqual(count, 0)
    def testBatching(self):
        self._StartRecording()
        with mayaUsdLib.DiagnosticBatchContext():
            Tf.Warn("spooky warning")
            Tf.Status("informative status")

            for i in range(5):
                Tf.Status("repeated status %d" % i)

            for i in range(3):
                Tf.Warn("spam warning %d" % i)

            try:
                Tf.RaiseCodingError("coding error!")
            except:
                pass
        log = self._StopRecording()

        # Note: we use assertItemsEqual because coalescing may re-order the
        # diagnostic messages.
        self.assertItemsEqual(log, [
            ("spooky warning", OM.MCommandMessage.kWarning),
            ("informative status", OM.MCommandMessage.kInfo),
            ("repeated status 0 -- and 4 similar", OM.MCommandMessage.kInfo),
            ("spam warning 0 -- and 2 similar", OM.MCommandMessage.kWarning)
        ])
    def testBatching_DelegateRemoved(self):
        """Tests removing the diagnostic delegate when the batch context is
        still open."""
        self._StartRecording()
        with mayaUsdLib.DiagnosticBatchContext():
            Tf.Warn("this warning won't be lost")
            Tf.Status("this status won't be lost")

            cmds.unloadPlugin('pxrUsd', force=True)

            for i in range(5):
                Tf.Status("no delegate, this will be lost %d" % i)
        log = self._StopRecording()

        # Note: we use assertItemsEqual because coalescing may re-order the
        # diagnostic messages.
        self.assertItemsEqual(log, [
            ("this warning won't be lost", OM.MCommandMessage.kWarning),
            ("this status won't be lost", OM.MCommandMessage.kInfo),
        ])